【发布时间】:2020-10-27 21:34:13
【问题描述】:
我在下面有这个 R 代码来填充嵌套列表:
my_information = list()
for(a in 1:(length(first_list))){
my_information[[paste0("first_list",a)]]=a
for(b in 1:(length(second_list))){
my_information[[paste0("first_list",a)]][[paste0("second_list",b)]] = second_list[[b]] - first_list[[a]]
if(my_information[[paste0("first_list",a)]][[paste0("second_list",b)]]>0){
for(c in 1:length(third_list)){
my_information[[paste0("first_list",a)]][[paste0("second_list",b)]][[paste0("third_list",c)]] = third_list[[c]] - second_list[[b]])
for(d in 1:length(fourth_list)){
my_information[[paste0("first_list",a)]][[paste0("second_list",b)]][[paste0("third_list",c)]][[paste0("fourth_list",d)]] = fourth_list[[d]] - third_list[c])
}
}
}
}
}
当我运行这段代码时,我得到了错误:
Error in my_information[[paste0("first_list",a)]][[paste0("second_list",b)]]<- `*vtmp*`
more elements supplied than there are to replace
我不确定如何在嵌套循环中解决这个问题,欢迎任何想法
【问题讨论】:
-
请使用
dput或我们可以复制和使用的东西添加数据。了解how to ask a good question 和how to give a reproducible example。
标签: r list nested-loops nested-lists