如何制作数据框列表?

如何制作数据框列表?

如何制作数据框列表以及如何从列表中访问每个数据框?


例如,如何将这些数据框放在列表中?


d1 <- data.frame(y1 = c(1, 2, 3),

                 y2 = c(4, 5, 6))

d2 <- data.frame(y1 = c(3, 2, 1),

                 y2 = c(6, 5, 4))


当年话下
浏览 783回答 4
4回答

慕后森

这与您的问题无关,但您想要使用=而不是<-在函数调用中。如果您使用<-,您将最终创建变量,y1并y2在您正在使用的任何环境中:d1 <- data.frame(y1 <- c(1, 2, 3), y2 <- c(4, 5, 6))y1# [1] 1 2 3y2# [1] 4 5 6这不会产生在数据框中创建列名的看似期望的效果:d1#&nbsp; &nbsp;y1....c.1..2..3. y2....c.4..5..6.# 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4# 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5# 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6该=运营商,在另一方面,将您的向量与参数相关联data.frame。至于您的问题,制作数据框列表很容易:d1 <- data.frame(y1 = c(1, 2, 3), y2 = c(4, 5, 6))d2 <- data.frame(y1 = c(3, 2, 1), y2 = c(6, 5, 4))my.list <- list(d1, d2)您可以像访问任何其他列表元素一样访问数据框:my.list[[1]]#&nbsp; &nbsp;y1 y2# 1&nbsp; 1&nbsp; 4# 2&nbsp; 2&nbsp; 5# 3&nbsp; 3&nbsp; 6

叮当猫咪

您也可以访问特定的列和值在每个列表元素与&nbsp; [和[[。这里有几个例子。首先,我们只能访问列表中每个数据框的第一列lapply(ldf, "[", 1),其中1表示列号。ldf <- list(d1 = d1, d2 = d2)&nbsp; ## create a named list of your data frameslapply(ldf, "[", 1)# $d1#&nbsp; &nbsp;y1# 1&nbsp; 1# 2&nbsp; 2# 3&nbsp; 3## $d2#&nbsp; &nbsp;y1# 1&nbsp; 3# 2&nbsp; 2# 3&nbsp; 1同样,我们可以访问第二列中的第一个值lapply(ldf, "[", 1, 2)# $d1# [1] 4#&nbsp;# $d2# [1] 6然后我们也可以直接访问列值,作为向量,用 [[lapply(ldf, "[[", 1)# $d1# [1] 1 2 3## $d2# [1] 3 2 1

慕丝7291255

如果您有大量按顺序命名的数据框,则可以创建所需数据框子集的列表,如下所示:d1 <- data.frame(y1=c(1,2,3), y2=c(4,5,6))d2 <- data.frame(y1=c(3,2,1), y2=c(6,5,4))d3 <- data.frame(y1=c(6,5,4), y2=c(3,2,1))d4 <- data.frame(y1=c(9,9,9), y2=c(8,8,8))my.list <- list(d1, d2, d3, d4)my.listmy.list2 <- lapply(paste('d', seq(2,4,1), sep=''), get)my.list2where my.list2返回包含第2,第3和第4个数据帧的列表。[[1]]&nbsp; y1 y21&nbsp; 3&nbsp; 62&nbsp; 2&nbsp; 53&nbsp; 1&nbsp; 4[[2]]&nbsp; y1 y21&nbsp; 6&nbsp; 32&nbsp; 5&nbsp; 23&nbsp; 4&nbsp; 1[[3]]&nbsp; y1 y21&nbsp; 9&nbsp; 82&nbsp; 9&nbsp; 83&nbsp; 9&nbsp; 8但请注意,上面列表中的数据框不再被命名。如果要创建包含数据框子集的列表并希望保留其名称,可以尝试以下操作:list.function <-&nbsp; function() {&nbsp;&nbsp; &nbsp; &nbsp;d1 <- data.frame(y1=c(1,2,3), y2=c(4,5,6))&nbsp; &nbsp; &nbsp;d2 <- data.frame(y1=c(3,2,1), y2=c(6,5,4))&nbsp; &nbsp; &nbsp;d3 <- data.frame(y1=c(6,5,4), y2=c(3,2,1))&nbsp; &nbsp; &nbsp;d4 <- data.frame(y1=c(9,9,9), y2=c(8,8,8))&nbsp; &nbsp; &nbsp;sapply(paste('d', seq(2,4,1), sep=''), get, environment(), simplify = FALSE)&nbsp;}&nbsp;my.list3 <- list.function()my.list3返回:> my.list3$d2&nbsp; y1 y21&nbsp; 3&nbsp; 62&nbsp; 2&nbsp; 53&nbsp; 1&nbsp; 4$d3&nbsp; y1 y21&nbsp; 6&nbsp; 32&nbsp; 5&nbsp; 23&nbsp; 4&nbsp; 1$d4&nbsp; y1 y21&nbsp; 9&nbsp; 82&nbsp; 9&nbsp; 83&nbsp; 9&nbsp; 8> str(my.list3)List of 3&nbsp;$ d2:'data.frame':&nbsp; &nbsp; &nbsp;3 obs. of&nbsp; 2 variables:&nbsp; ..$ y1: num [1:3] 3 2 1&nbsp; ..$ y2: num [1:3] 6 5 4&nbsp;$ d3:'data.frame':&nbsp; &nbsp; &nbsp;3 obs. of&nbsp; 2 variables:&nbsp; ..$ y1: num [1:3] 6 5 4&nbsp; ..$ y2: num [1:3] 3 2 1&nbsp;$ d4:'data.frame':&nbsp; &nbsp; &nbsp;3 obs. of&nbsp; 2 variables:&nbsp; ..$ y1: num [1:3] 9 9 9&nbsp; ..$ y2: num [1:3] 8 8 8> my.list3[[1]]&nbsp; y1 y21&nbsp; 3&nbsp; 62&nbsp; 2&nbsp; 53&nbsp; 1&nbsp; 4> my.list3$d4&nbsp; y1 y21&nbsp; 9&nbsp; 82&nbsp; 9&nbsp; 83&nbsp; 9&nbsp; 8
打开App,查看更多内容
随时随地看视频慕课网APP