我为这个有点混乱的标题道歉(欢迎任何改进建议)..
假设我有一个列表,其中包含几个(例如四个)列表,稍后我想在其中存储 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")})
然而,这并没有给我同样的结果,但我似乎无法弄清楚我在这里忽略了什么。有什么建议么?
谢谢!