我正在阅读一系列以数据帧列表结尾的文件。之后,我有兴趣添加一些与每个数据框相关的其他信息。因此,我想在数据框列表的每个元素中添加一些其他元素。
我的尝试是实际构建“多余的东西”列表,然后尝试将其与数据框列表合并。
示例代码:
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)
我使用@Я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 ...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句