我对这个有点令人困惑的标题表示道歉(欢迎提出任何改进建议)..
假设我有一个列表,其中包含几个(例如四个)列表,我希望稍后在其中存储20个对象:
mylist <- vector(mode="list",length=4) names(mylist) <- c("One","Two","Three","Four") mylist$One <- mylist$Two <- mylist$Three <- mylist$Four <- vector(mode="list", length=20)我想事先定义这些对象的名称。 当然,我可以这样做:
names(mylist$One) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T") names(mylist$Two) <- names(mylist$Three) <- names(mylist$Four) <- names(mylist$One)但是如果列表的数量会增加(就像在我的实际数据中那样),这会变得相当麻烦,所以我试图用lapply这样的函数来做这个:
mylist <- lapply(mylist,FUN=function(x) {names(x) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T")})然而,这并没有给我相同的结果,但我似乎无法弄清楚我在这里忽略了什么。 有什么建议么?
谢谢!
My apologies for the somewhat confusing title (any suggestion for improvement are welcome)..
Suppose I have a list which contains several (e.g. four) lists in which I would like to store 20 objects later on:
mylist <- vector(mode="list",length=4) names(mylist) <- c("One","Two","Three","Four") mylist$One <- mylist$Two <- mylist$Three <- mylist$Four <- vector(mode="list", length=20)I would like to define the names of those objects beforehand. Of course, I can do that as following:
names(mylist$One) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T") names(mylist$Two) <- names(mylist$Three) <- names(mylist$Four) <- names(mylist$One)But if the number of the lists would increase (as is the case in my actual data), this becomes rather cumbersome, so I was trying to do this with a function such as lapply :
mylist <- lapply(mylist,FUN=function(x) {names(x) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T")})This, however, does not give me the same result, but I can not seem to figure out what I am overlooking here. Any suggestions?
Thanks!
最满意答案
您需要在lapply调用中返回一个值:
mylist <- lapply(mylist,FUN=function(x) {names(x) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T") x ## <- note the x here; you could also use return(x) }) mylist # $One # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Two # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Three # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Four # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"You need to return a value in your lapply call:
mylist <- lapply(mylist,FUN=function(x) {names(x) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T") x ## <- note the x here; you could also use return(x) }) mylist # $One # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Two # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Three # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Four # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"更多推荐
发布评论