Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 向每个列表元素添加数据帧_R_List_Dataframe - Fatal编程技术网

R 向每个列表元素添加数据帧

R 向每个列表元素添加数据帧,r,list,dataframe,R,List,Dataframe,我正在读取一系列文件,这些文件最终显示在数据帧列表中。在完成这项工作之后,我感兴趣的是将一些与每个数据帧相关的附加信息放进去。因此,我想在dataframe列表的每个元素中添加一些额外的元素 我的尝试是实际构建“额外内容”列表,然后尝试将其与数据帧列表合并 示例代码: set.seed(42) #Building my list of data.frames. In my specific case this is coming from files A <- data.frame(x=

我正在读取一系列文件,这些文件最终显示在数据帧列表中。在完成这项工作之后,我感兴趣的是将一些与每个数据帧相关的附加信息放进去。因此,我想在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)
set.seed(42)
#正在构建我的data.frames列表。在我的具体情况下,这是来自文件

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

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

ListD好的,我想这对于
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

我使用了@Я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]]
}
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 ...