将数据帧添加到每个列表元素

时间:2013-11-20 08:52:16

标签: r list dataframe

我正在阅读最终列在数据帧列表中的一系列文件。在这之后,我有兴趣提供与每个数据帧相关的一些额外信息。所以,我想添加到我的数据框列表的每个元素,一些额外的元素。

我的尝试是实际构建“额外内容”列表,然后尝试将其与数据帧列表合并。

示例代码:

set.seed(42)

#Building my list of data.frames. In my specific case this is coming from files
A <- data.frame(x=rnorm(10), y=rnorm(10))
B <- data.frame(x=rnorm(10), y=rnorm(10))

ListD <- list(A, B)
names(ListD)<- c("A", "B") #some names to know what is what

#now my attributes. Each data.frame as some properties that i want to keep track of.
newList <- list(A=c("Color"=123, "Date"=321), B=c("Color"=111, "Date"=111))

#My wished output is a list were each element of the list has 
#"Color", "Date" and a dataframe
#I tried something like:
lapply(ListD, append, values=newList)

3 个答案:

答案 0 :(得分:1)

据我所知,您所要做的就是将ListD的初始化更改为:

ListD <- list(list(A), list(B))

因为您需要的数据结构是列表列表 - 内部列表包含data.frame和另外两个属性。我无法保证这正是您所希望的结果,但目前这就是您的问题所在。

答案 1 :(得分:1)

好吧,我认为这对于mapply来说会很简单,但是我无法让这些列表很好地一起玩......也许别人可以。所以这是一个for解决方案:

#preallocate list
updatedList <- vector(mode = "list", length = length(ListD)) 
names(updatedList) <- names(ListD)
for(i in 1:length(updatedList)) {
  updatedList[[i]] <- c(ListD[i], newList[[i]])
}
updatedList$A
# $A
# x          y
# 1  -0.51690823  0.4521443
# 2   0.97544933 -0.7212561
# 3   0.98909668 -0.2258737
# 4  -1.72753947 -0.7643175
# 5  -1.31050478 -3.2526437
# 6  -0.63845053  1.1263407
# 7  -0.09010858 -0.9386608
# 8  -0.53933869 -0.6882866
# 9   0.54668290  1.7227261
# 10 -0.87948586 -0.2413344
# 
# $Color
# [1] 123
# 
# $Date
# [1] 321

或者,如果您接受@Яaffael的建议,mapply有效,但这将取决于您首先从文件构建列表:

ListD <- list(list(A), list(B))
updatedList <- mapply(c, ListD, newList, SIMPLIFY = FALSE)

答案 2 :(得分:0)

我使用@Яaffael建议列表。

由于改变我的数据帧列表并不是很容易,因为我从文件中读取它们的方式,我用额外的数据制作了列表列表,然后像这样加入数据帧:

newList <- list(A=list("Color"=123, "Date"=321), B=list("Color"=111, "Date"=111))

for(n in names(newList)){
  newList[[n]]$Dataframe <- ListD[[n]]
}

我的输出结构:

> str(newList)

List of 2
 $ A:List of 3
  ..$ Color    : num 123
  ..$ Date     : num 321
  ..$ Dataframe:'data.frame':   10 obs. of  2 variables:
  .. ..$ x: num [1:10] 1.371 -0.565 0.363 0.633 0.404 ...
  .. ..$ y: num [1:10] 1.305 2.287 -1.389 -0.279 -0.133 ...
 $ B:List of 3
  ..$ Color    : num 111
  ..$ Date     : num 111
  ..$ Dataframe:'data.frame':   10 obs. of  2 variables:
  .. ..$ x: num [1:10] -0.307 -1.781 -0.172 1.215 1.895 ...
  .. ..$ y: num [1:10] 0.455 0.705 1.035 -0.609 0.505 ...
相关问题