将数据帧列表转换为单个数据帧并更改列名称 [英] Converting a list of data frames to a single data frame and change column names

查看:109
本文介绍了将数据帧列表转换为单个数据帧并更改列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据显示如下,使用 dput

My data is presented below using dput.

dat <- structure(list(`60` = structure(c(25.2142857142857, 25.2142857142857, 
25.2142857142857, 16.8333333333333, 6.18181818181818, 6.45454545454545, 
39.3076923076923, 17.8, 30.2307692307692, 31.9090909090909, 338.872342659885, 
338.872342659885, 338.872342659885, 312.566239187662, 108.98770426029, 
132.000329498368, 295.499325777881, 289.05210119046, 279.319320138619, 
282.696361655751), .Dim = c(10L, 2L), .Dimnames = list(NULL, 
    c("CanCov", "Aspect"))), `100` = structure(c(22.2285714285714, 
21.8285714285714, 22.2285714285714, 17.4285714285714, 7.54054054054054, 
5.51351351351351, 32.8823529411765, 18.0285714285714, 31.3125, 
32.5833333333333, 328.300126247896, 336.611388179775, 328.300126247896, 
288.830157290819, 132.674633942446, 122.597267778504, 295.162359106757, 
254.508961455896, 280.326744650874, 287.386617538886), .Dim = c(10L, 
2L), .Dimnames = list(NULL, c("CanCov", "Aspect"))), `500` = structure(c(10.786941580756, 
10.7688787185355, 10.8489702517162, 10.7628278221209, 14.1569301260023, 
12.9438717067583, 12.8735632183908, 10.8551724137931, 20.729667812142, 
23.3722794959908, 195.270942450807, 195.540990751048, 195.662725661548, 
190.688980052674, 165.038240066186, 133.772446928244, 198.45485951978, 
188.942107644257, 203.862336021767, 217.567077176237), .Dim = c(10L, 
2L), .Dimnames = list(NULL, c("CanCov", "Aspect"))), `1000` = structure(c(10.3804067602406, 
10.3746059042706, 10.381156930126, 9.8993981083405, 13.26243567753, 
13.6912732474964, 11.3125, 9.73461208130547, 17.5430539609644, 
18.8537492844877, 174.841410186063, 174.803449739022, 174.777413321887, 
169.181037352303, 148.07213983955, 145.460198642085, 157.562633627451, 
162.484978829108, 159.688505118645, 163.433969343022), .Dim = c(10L, 
2L), .Dimnames = list(NULL, c("CanCov", "Aspect")))), .Names = c("60", 
"100", "500", "1000"))

我有一个包含四个元素(名称为60,100,5500和1000)的列表。

I have a list with four elements (named 60, 100, 500, and 1000).

> str(dat)
List of 4
 $ 60  : num [1:10, 1:2] 25.21 25.21 25.21 16.83 6.18 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "CanCov" "Aspect"
 $ 100 : num [1:10, 1:2] 22.23 21.83 22.23 17.43 7.54 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "CanCov" "Aspect"
 $ 500 : num [1:10, 1:2] 10.8 10.8 10.8 10.8 14.2 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "CanCov" "Aspect"
 $ 1000: num [1:10, 1:2] 10.4 10.4 10.4 9.9 13.3 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "CanCov" "Aspect"

制作一个8×10的数据帧,其中列对应于列表名称。具体来说,这些列将被标记为CanCov_60,Aspect_60,CanCov_100,... CanCov_1000,Aspect_1000,其中CanCov_60和Aspect_60的值将来自具有相应名称的列表。

I want to make a single 8 by 10 data frame where the columns correspond to the list name. Specifically, the columns would be labeled CanCov_60, Aspect_60, CanCov_100, ... ,CanCov_1000, Aspect_1000, where the values for CanCov_60 and Aspect_60 would come from the list with the corresponding name.

我怀疑 ldply 是使用最好的功能,但似乎无法连接点。

I suspect ldply is the best function to use but cannot seem to connect the dots.

推荐答案

您可以在base-R中使用您的列表名称上的lapply来执行此操作。首先,我们检索特定的数据框,然后更改列名并返回。最后,我们使用 do.call(cbind(...))创建结果。

You can do this in base-R, by using lapply on your list names. First, we retrieve that specific dataframe, then we change the column names and return it. Finally, we use do.call(cbind(...)) to create the result.

编辑:我去了题目的标题,假设所有的对象都是数据框,没有检查。但是,他们实际上是矩阵,如@Damianofantini指出的。我已经向data.frame添加了一个转换。

I went by the title of the question and assumed all objects were dataframes and didnt check. However, they were in fact matrices, as @Damianofantini pointed out. I've added a conversion to data.frame.

do.call(cbind,lapply(names(dat),function(x){
  res <- dat[[x]]
  colnames(res) <- paste(colnames(res),x,sep="_")
  data.frame(res)
}))

这篇关于将数据帧列表转换为单个数据帧并更改列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆