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

时间:2021-10-13 14:30:57

I am reading a series of files that end up in a list of dataframes. After doing that, i'm interested in putting some additional information related to each dataframe. So, I want to add to each element of my dataframe list, some additional elements.

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

My attempt was to actually build the list of "extra stuff" and then try to merge it with the list of dataframes.

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

Example code:

示例代码:

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 个解决方案

#1


1  

As far as I can tell all you have to do is change you're initialization of ListD to:

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

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

Because the data structure you want is a list of lists - with the inner lists holding a data.frame and two further attributes. I can't gurantee this is exactly the result you desire but essently this is where your problem is located.

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

#2


1  

OK, I thought this would be straightforward with mapply, but I can't get the lists to play together well... maybe someone else can. So here's a for solution:

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

#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

Alternatively, if you take @Яaffael's suggestion, mapply works, but that will depend how you're building the list from the files in the first place:

或者,如果您采用@Яaffael的建议,mapply可以工作,但这将取决于您首先从文件构建列表:

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

#3


0  

I used @Яaffael suggestion of a list of lists.

我用@Яaffael建议列表清单。

Since changing my list of dataframes is not very easy, because of the way i'm reading them from files, I made the list of lists with the extra data, and then join the dataframes like this:

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

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

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

the structure of my output:

我输出的结构:

> 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 ...

#1


1  

As far as I can tell all you have to do is change you're initialization of ListD to:

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

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

Because the data structure you want is a list of lists - with the inner lists holding a data.frame and two further attributes. I can't gurantee this is exactly the result you desire but essently this is where your problem is located.

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

#2


1  

OK, I thought this would be straightforward with mapply, but I can't get the lists to play together well... maybe someone else can. So here's a for solution:

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

#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

Alternatively, if you take @Яaffael's suggestion, mapply works, but that will depend how you're building the list from the files in the first place:

或者,如果您采用@Яaffael的建议,mapply可以工作,但这将取决于您首先从文件构建列表:

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

#3


0  

I used @Яaffael suggestion of a list of lists.

我用@Яaffael建议列表清单。

Since changing my list of dataframes is not very easy, because of the way i'm reading them from files, I made the list of lists with the extra data, and then join the dataframes like this:

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

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

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

the structure of my output:

我输出的结构:

> 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 ...