我想初始化N个命名的空数据帧,每个数据帧中有X行和Y列,并将它们放在一个列表中。 稍后我将通过添加计算向量来填充它们。 这是我所知道的一个新手问题,但是如何做到这一点呢? 例如:
my.names <- c("G", "H", "J") N <- 3 my.list <- ??? ## how?(根据回复编辑...)我采用这种方法的原因是我有一个大循环,每次循环都会创建N个向量。 我需要在每次迭代时保存这些结果。 我计划做类似的事情:
for(i in 1:N) my.list[[i]] <- my.vec[i]在循环中的每个传递。 3D数据框或矩阵也可以做同样的事情。
I want to initialize N named, empty data frames that have X rows and Y columns in each and put them in a list. I will fill them later by adding computed vectors. It’s a newbie question I know, but how does one do that? For example:
my.names <- c("G", "H", "J") N <- 3 my.list <- ??? ## how?(edited in light of replies ...) My reason for this approach is I have a big loop that creates N vectors each time through the loop. I need to save these results each iteration. I planned to do something like:
for(i in 1:N) my.list[[i]] <- my.vec[i]on each pass in the loop. A 3D data frame or matrix would also do the same thing.
最满意答案
也许你想初始化一个三维array呢?
myarr <- array(,dim=c(3,5,10)) for (j in 1:5) for (k in 1:10) myarr[,j,k] <- rnorm(3)空data.frames列表。 我不认为有一个很好的理由来初始化data.frames列表,但是这里是如何完成的:
data.frame()创建一个“空”data.frame:
setNames(replicate(3,data.frame()),my.names) # $G # data frame with 0 columns and 0 rows # $H # data frame with 0 columns and 0 rows # $J # data frame with 0 columns and 0 rows包含行和列的data.frame必须具有一些值,例如NA :
nr <- 4 nc <- 4 ndf <- length(my.names) setNames(replicate(ndf,as.data.frame(matrix(,nr,nc)),simplify=FALSE),my.names)Maybe you want to initialize a three-dimensional array instead?
myarr <- array(,dim=c(3,5,10)) for (j in 1:5) for (k in 1:10) myarr[,j,k] <- rnorm(3)List of empty data.frames. I don't think there's a good reason to initialize a list of data.frames, but here's how it would be done:
An "empty" data.frame is created by data.frame():
setNames(replicate(3,data.frame()),my.names) # $G # data frame with 0 columns and 0 rows # $H # data frame with 0 columns and 0 rows # $J # data frame with 0 columns and 0 rowsA data.frame with rows and columns must have some values, for example NA:
nr <- 4 nc <- 4 ndf <- length(my.names) setNames(replicate(ndf,as.data.frame(matrix(,nr,nc)),simplify=FALSE),my.names)更多推荐
发布评论