我正在阅读一系列文件,这些文件最终出现在数据帧列表中。这样做之后,我有兴趣添加一些与每个数据框相关的附加信息。因此,我想向我的数据框列表的每个元素添加一些额外的元素。
我的尝试是实际构建“额外内容”列表,然后尝试将其与数据帧列表合并。
示例代码:
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)
最佳答案
据我所知,您所要做的就是将 ListD 的初始化更改为:
ListD <- list(list(A), list(B))
因为您想要的数据结构是列表的列表 - 内部列表包含一个 data.frame 和另外两个属性。我不能保证这正是您想要的结果,但基本上这就是您的问题所在。
关于r - 将数据框添加到每个列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20091224/