list 多维清单?名单清单?列表数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15046801/
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
multi-dimensional list? List of lists? array of lists?
提问by Vasily A
(I am definitively using wrong terminology in this question, sorry for that - I just don't know the correct way to describe this in R terms...)
(我在这个问题中明确使用了错误的术语,抱歉 - 我只是不知道用 R 术语描述这个的正确方法......)
I want to create a structure of heterogeneous objects. The dimensions are not necessary rectangular. What I need would be probably called just "array of objects" in other languages like C. By 'object' I mean a structure consisting of different members, i.e. just a list in R - for example:
我想创建一个异构对象的结构。尺寸不一定是矩形。我需要的可能在 C 等其他语言中被称为“对象数组”。“对象”是指由不同成员组成的结构,即 R 中的一个列表 - 例如:
myObject <- list(title="Uninitialized title", xValues=rep(NA,50), yValues=rep(NA,50))
and now I would like to make 100 such objects, and to be able to address their members by something like
现在我想制作 100 个这样的对象,并且能够通过类似的方式解决它们的成员
for (i in 1:100) {myObject[i]["xValues"]<-rnorm(50)}
or
或者
for (i in 1:100) {myObject[i]$xValues<-rnorm(50)}
I would be grateful for any hint about where this thing is described.
我将不胜感激任何有关描述这件事的地方的提示。
Thanks in advance!
提前致谢!
采纳答案by Arun
I did not realize that you were looking for creating other objects of same structure. You are looking for replicate
.
我没有意识到您正在寻找创建相同结构的其他对象。您正在寻找replicate
.
my_fun <- function() {
list(x=rnorm(1), y=rnorm(1), z="bla")
}
replicate(2, my_fun(), simplify=FALSE)
# [[1]]
# [[1]]$x
# [1] 0.3561663
#
# [[1]]$y
# [1] 0.4795171
#
# [[1]]$z
# [1] "bla"
#
#
# [[2]]
# [[2]]$x
# [1] 0.3385942
#
# [[2]]$y
# [1] -2.465932
#
# [[2]]$z
# [1] "bla"
回答by Anthony Damico
are you looking for the name of this mythical beast or just how to do it? :) i could be wrong, but i think you'd just call it a list of lists.. for example:
你是在寻找这个神话野兽的名字还是怎么做?:) 我可能是错的,但我认为你只是将它称为列表列表.. 例如:
# create one list object
x <- list( a = 1:3 , b = c( T , F ) , d = mtcars )
# create a second list object
y <- list( a = c( 'hi', 'hello' ) , b = c( T , F ) , d = matrix( 1:4 , 2 , 2 ) )
# store both in a third object
z <- list( x , y )
# access x
z[[ 1 ]]
# access y
z[[ 2 ]]
# access x's 2nd object
z[[ 1 ]][[ 2 ]]
回答by smci
If the dimensions are always going to be rectangular (in your case, 100x50), and the contents are always going to be homogeneous (in your case, numeric) then create a 2D array/matrix.
如果尺寸始终是矩形(在您的情况下为 100x50),并且内容始终是同质的(在您的情况下为数字),则创建一个 2D array/matrix。
If you want the ability to add/delete/insert on individual lists (or change the data type), then use a list-of-lists.
如果您希望能够在单个列表上添加/删除/插入(或更改数据类型),请使用list-of-lists。
回答by Vasily A
here is the example of solution I have for the moment, maybe it will be useful for somebody:
这是我目前的解决方案示例,也许它对某人有用:
NUM <- 1000 # NUM is how many objects I want to have
xVal <- vector(NUM, mode="list")
yVal <- vector(NUM, mode="list")
title <- vector(NUM, mode="list")
for (i in 1:NUM) {
xVal[i]<-list(rnorm(50))
yVal[i]<-list(rnorm(50))
title[i]<-list(paste0("This is title for instance #", i))
}
myObject <- list(xValues=xVal, yValues=yVal, titles=title)
# now I can address any member, as needed:
print(myObject$titles[[3]])
print(myObject$xValues[[4]])