在R中的循环中构建列表-获得正确的项目名称

在R中的循环中构建列表-获得正确的项目名称,r,list,loops,R,List,Loops,我有一个函数,它包含两个列表上的循环,并建立一些计算数据。我想以列表的形式返回这些数据,通过一些值进行索引,但我的赋值错误 关于我正在尝试做的事情以及我出错的地方,一个最简单的例子是: mybiglist <- list() for(i in 1:5){ a <- runif(10) b <- rnorm(16) c <- rbinom(8, 5, i/10) name <- paste('item:',i,sep='') t

我有一个函数,它包含两个列表上的循环,并建立一些计算数据。我想以列表的形式返回这些数据,通过一些值进行索引,但我的赋值错误

关于我正在尝试做的事情以及我出错的地方,一个最简单的例子是:

mybiglist <- list()
for(i in 1:5){
    a <- runif(10)
    b <- rnorm(16)
    c <- rbinom(8, 5, i/10)
    name <- paste('item:',i,sep='')
    tmp <- list(uniform=a, normal=b, binomial=c)
    mybiglist[[name]] <- append(mybiglist, tmp)
}

mybiglist如果不使用
append
命令:

mybiglist <- list()
for(i in 1:5){
  a <- runif(10)
  b <- rnorm(16)
  c <- rbinom(8, 5, i/10)
  name <- paste('item:',i,sep='')
  tmp <- list(uniform=a, normal=b, binomial=c)
  mybiglist[[name]] <- tmp
}

# List of 5
# $ item:1:List of 3
# ..$ uniform : num [1:10] 0.737 0.987 0.577 0.814 0.452 ...
# ..$ normal  : num [1:16] -0.403 -0.104 2.147 0.32 1.713 ...
# ..$ binomial: num [1:8] 0 0 0 0 1 0 0 1
# $ item:2:List of 3
# ..$ uniform : num [1:10] 0.61 0.62 0.49 0.217 0.862 ...
# ..$ normal  : num [1:16] 0.945 -0.154 -0.5 -0.729 -0.547 ...
# ..$ binomial: num [1:8] 1 2 2 0 2 1 0 2
# $ item:3:List of 3
# ..$ uniform : num [1:10] 0.66 0.094 0.432 0.634 0.949 ...
# ..$ normal  : num [1:16] -0.607 0.274 -1.455 0.828 -0.73 ...
# ..$ binomial: num [1:8] 2 2 3 1 1 1 2 0
# $ item:4:List of 3
# ..$ uniform : num [1:10] 0.455 0.442 0.149 0.745 0.24 ...
# ..$ normal  : num [1:16] 0.0994 -0.5332 -0.8131 -1.1847 -0.8032 ...
# ..$ binomial: num [1:8] 2 3 1 1 2 2 2 1
# $ item:5:List of 3
# ..$ uniform : num [1:10] 0.816 0.279 0.583 0.179 0.321 ...
# ..$ normal  : num [1:16] -0.036 1.137 0.178 0.29 1.266 ...
# ..$ binomial: num [1:8] 3 4 3 4 4 2 2 3
mybiglist更改


mybiglist[[name]]以显示不需要显式for循环

unif_norm  <- replicate(5, list(uniform = runif(10),
  normal = rnorm(16)), simplify=F)

binomials <- lapply(seq_len(5)/10, function(prob) {
 list(binomial =  rbinom(n = 5 ,size = 8, prob = prob))})

biglist <- setNames(mapply(c, unif_norm, binomials, SIMPLIFY = F), 
                     paste0('item:',seq_along(unif_norm)))

c
命名对象不是一件好事!!谢谢虽然我不想实际创建均匀分布、正态分布和二项分布的列表;-)这是我追求的命名惯例。至少记得预先分配!你能解释一下为什么在
mybiglist[[name]]@spops中,双括号用于访问列表元素吗。单括号返回列表。这就是为什么当与
[[]]
一起使用时,
追加
失败的原因吗?因为您不能附加到元素,但可以附加到列表?(我一直在研究如何将项目添加到列表中,偶然发现了这篇文章,我一直在想)@spops
append
失败,因为整个列表都被用来替换其中一个元素。@SvenHohenstein-谢谢你的回答。有没有办法找出两个
mybiglist
mybiglist\u a
mybiglist\u B
)之间的百分比差异?我可以使用
idential()
来获取
mybiglist\u A
是否与
mybiglist\u B
完全相同,但不知道是否可以找到百分比差异?
mybiglist[[name]] <- tmp
unif_norm  <- replicate(5, list(uniform = runif(10),
  normal = rnorm(16)), simplify=F)

binomials <- lapply(seq_len(5)/10, function(prob) {
 list(binomial =  rbinom(n = 5 ,size = 8, prob = prob))})

biglist <- setNames(mapply(c, unif_norm, binomials, SIMPLIFY = F), 
                     paste0('item:',seq_along(unif_norm)))
mybiglist <- vector('list', 5)
names(mybiglist) <- paste0('item:', seq_along(mybiglist))
for(i in seq_along(mybiglist)){
    a <- runif(10)
    b <- rnorm(16)
    c <- rbinom(8, 5, i/10)

    tmp <- list(uniform=a, normal=b, binomial=c)
    mybiglist[[i]] <- tmp
}