如何将N个命名的空数据帧初始化为R中的列表?(How to initialize N named, empty data frames into a list in R?)

编程入门 行业动态 更新时间:2024-10-23 01:58:33
如何将N个命名的空数据帧初始化为R中的列表?(How to initialize N named, empty data frames into a list in R?)

我想初始化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 rows

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

更多推荐

本文发布于:2023-07-16 07:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125463.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   数据   列表   initialize   named

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!