当前位置:  开发笔记 > 编程语言 > 正文

将数据框列表转换为单个数据框并更改列名称

如何解决《将数据框列表转换为单个数据框并更改列名称》经验,为你挑选了1个好方法。

我的数据如下所示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,500和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的值将来自具有相应名称的列表.

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



1> Heroka..:

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

编辑:我按照问题的标题,并假设所有对象都是数据帧,并没有检查.然而,正如@Damianofantini指出的那样,它们实际上就是矩阵.我已经添加了对data.frame的转换.

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

推荐阅读
mobiledu2402851373
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有