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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12511648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:53:39  来源:igfitidea点击:

Building a list in a loop in R - getting item names correct

rlistloops

提问by Hassantm

I have a function which contains a loop over two lists and builds up some calculated data. I would like to return these data as a lists of lists, indexed by some value, but I'm getting the assignment wrong.

我有一个函数,它包含两个列表的循环并构建一些计算数据。我想将这些数据作为列表列表返回,由某个值索引,但我的分配错误。

A minimal example of what I'm trying to do, and where i'm going wrong would be:

我正在尝试做的一个最小的例子,以及我出错的地方是:

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)
}

If you run this and look at the output mybiglist, you will see that something is going very wrong in the way each item is being named.

如果您运行它并查看输出 mybiglist,您会发现每个项目的命名方式都出现了问题。

Any ideas on how I might achieve what I actually want?

关于如何实现我真正想要的东西的任何想法?

Thanks

谢谢

ps. I know that in R there is a sense in which one has failed if one has to resort to loops, but in this case I do feel justified ;-)

附:我知道在 R 中,如果不得不求助于循环,则有一种失败的感觉,但在这种情况下,我确实觉得有道理;-)

回答by Sven Hohenstein

It works if you don't use the appendcommand:

如果您不使用以下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

回答by seancarmody

Change

改变

mybiglist[[name]] <- append(mybiglist, tmp)

to

mybiglist[[name]] <- tmp

回答by mnel

To show that an explicit for loop is not required

显示不需要显式 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)))

In general if you go down the forloop path it is better to preassign the list beforehand. This is more memory efficient.

一般来说,如果你沿着for循环路径走,最好事先预先分配列表。这样内存效率更高。

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
}