【发布时间】:2020-06-11 16:09:11
【问题描述】:
我有一个 ini 文件,由 R 读取为列表(在示例 l 中)。现在我想沿向量 (m) 添加更多子列表,并始终为它们分配相同的常数。到目前为止我的尝试:
l <- list("A")
m <- letters[1:5]
n <- 5
for (i in 1:5){
assign(paste0("l$A$",m[i]), n)
}
# which does not work
# example of the desired outcome:
> l$A$e
[1] 5
我认为我还没有完全理解列表的工作原理......
【问题讨论】:
-
避免在 R:
l$A <- setNames(rep(list(n), length(m)), m)中使用assign。只需使用分配运算符<-扩展列表元素。