【发布时间】:2021-01-04 17:57:57
【问题描述】:
示例如下:
R<-vector("list",10) #a list of 10 elements
r<-rep(0,5) # each list includes this vector
#we add the vector to all lists
for(i in 1:10)
{
R[[i]]$r=r
}
index<-c(2,3,6)
现在我要做的是在index 指定的所有R 列表中将r[1] 更改为2。如果我们有一个大小为 1 的索引,那么解决方案将是 R[[index]]$r[1]=2。但是当我们有一系列索引时呢?实现这一目标的最有效方法是什么?
【问题讨论】:
-
您不想再次使用您的循环代码吗?
for(i in index) R[[i]]$r[1] <- 2 -
我一直在寻找比循环更有效的方法。另外,如果你能告诉我如何用更有效的方法替换第一个循环,那就太好了。
-
@Milad 这将与您的循环执行相同的操作:
map(1:10, ~list(r=r))