用for循环或Lapply填充列表

时间:2018-08-27 12:50:02

标签: r

这是我的列表:

mylist=dput(mylist)
list(list(
        c(30, 50, 35, 25, 45), 
        c(40, 35, 35, 50, 45), 
        c(40, 20, 40, 50, 25), 
    ),
    list(
        c(50, 50, 25, 40, 45, 40, 35, 40, 45, 20), 
        c(40, 35, 40, 40, 45, 30, 20, 50, 35, 25), 
        c(20, 30, 50, 35, 45, 40, 25, 50, 35, 50), 
    ),
    list(
        c(45, 50, 25, 25, 30, 25, 35, 35, 35, 30, 50, 50, 30, 30, 20), 
        c(40, 20, 35, 35, 50, 20, 25, 30, 35, 20, 40, 20, 45, 30, 20), 
        c(50, 20, 25, 35, 35, 30, 50, 25, 40, 35, 45, 45, 35, 45, 25), 
    ), 
    list(
        c(50, 50, 50, 40, 20, 25, 50, 40, 50, 50, 45, 40, 30, 50, 35, 45, 50, 30, 
35, 45), 
        c(45, 20, 25, 20, 25, 30, 20, 30, 45, 25, 50, 30, 30, 25, 50, 45, 20, 45, 45, 50), 
        c(20, 40, 50, 25, 40, 45, 25, 30, 20, 20, 35, 45, 20, 40, 50, 45, 40, 40, 45, 35), 
    ),
     list(
        c(35, 25, 45, 20, 25, 30, 30,  35, 30, 40, 30, 20, 20, 30, 45, 40, 35, 35, 35, 35, 25, 45, 35, 20, 50), 
        c(50, 35, 30, 30, 35, 45, 45, 50, 25, 25, 40, 25, 50, 45, 25, 30, 30, 25, 45, 45, 30, 20, 50, 30, 30), 
        c(35, 40, 50, 25, 40, 45, 30, 25, 50, 25, 35, 50, 50, 50, 25, 50, 20, 50, 40, 25, 25, 35, 20, 20, 50),
    )
)

mylist=dput(mylist)
list
    (
    list( c(30, 50, 35, 25, 45), 
          c(40, 35, 35, 50, 45), 
          c(40, 20, 40, 50, 25), 
    ),
    list(   
            c(50, 50, 25, 40, 45, 40, 35,  40, 45, 20), 
            c(40, 35, 40, 40, 45, 30, 20, 50, 35, 25), 
            c(20,  30, 50, 35, 45, 40, 25, 50, 35, 50),
    ), list(
            c(45, 50, 25, 25, 30, 25, 35, 35, 35, 30, 50, 50, 30, 30, 20), 
            c(40, 20, 35, 35, 50, 20, 25, 30, 35, 20, 40, 20, 45, 30, 20), 
            c(50, 20, 25, 35, 35, 30, 50, 25, 40, 35, 45, 45, 35, 45, 25), 
), 
       list(
            c(50, 50,50, 40, 20, 25, 50, 40, 50, 50, 45, 40, 30, 50, 35, 45, 50, 30, 35, 45), 
            c(45, 20, 25, 20, 25, 30, 20, 30, 45, 25, 50, 30, 30, 25, 50, 45, 20, 45, 45, 50), 
            c(20, 40, 50, 25, 40, 45, 25, 30, 20, 20, 35, 45, 20, 40, 50, 45, 40, 40, 45, 35), 
),
 list(
        c(35, 25, 45, 20, 25, 30, 30, 35, 30, 40, 30, 20, 20, 30, 45, 40, 35, 35, 35, 35, 25, 45, 35, 20, 50), 
        c(50, 35, 30, 30, 35, 45, 45, 50, 25, 25, 40, 25, 50, 45, 25, 30, 30, 25, 45, 45, 30, 20, 50, 30, 30), 
c(35, 40, 50, 25, 40, 45, 30, 25, 50, 25, 35, 50, 50, 50, 25, 50, 20, 50, 40, 25, 25, 35, 20, 20, 50),

 )))

我面临两个问题:

首先:我无法在下面运行此For循环:

resultlist<-vector(mode = "list", 5)

for (i in 1:6) {
  for(k in 1:5) {     

    resultlist[[k]][[i]]<-mean(mylist[[k]][[i]]) 
   }

}

它发送消息:Error in mylist[[k]][[i]] : subscript out of bounds

第二个问题实际上是一个建议:我原来的for大得多(例如,我的i一直到4828),然后用这2运行for索引(ki)中还有其他函数可以使我的代码更简单/有效,或者在这种情况下,最好的办法是保持for loop

2 个答案:

答案 0 :(得分:3)

由于mylist的第四个元素只有5个子列表,因此您收到该错误。

您也可以这样做:

resultlist <- lapply(mylist, function(x) lapply(x, function(y) rep(mean(y), length(y))))

得到想要的东西。

答案 1 :(得分:1)

在您的情况下,您的mylist[[4]]仅包含5个元素,因此发生错误是因为调用了它不存在的第六个元素。 如果所有列表元素的长度相等,则可以使用expand.grid查找所有组合,并使用sapply执行循环,该循环比for快一点 试试这个:

list_len=length(mylist)
sub_list_len=6
combination<-expand.grid(1:sub_list_len,1:list_len)
temp_output<-apply(combination,1,function(x) mean(  mylist[[x[2]]][[x[1]]] ))
resultlist<-split(temp_output,rep(1:list_len,each=sub_list_len)) %>% lapply(.,function(x) split(x,1:length(x)))
resultlist