繁体   English   中英

R数据框展平/整形

[英]R data frame flattening/reshape

我在形式R的数据帧df

a,1
a,4
a,2
b,6
b,8
b,4
c,4
c,5
c,2

我想以表格形式表示df

a,1,4,2
b,6,8,4
c,4,5,2

在R中进行此转换的更快方法是什么,特别是如果我的数据帧较大时?

通过使用dplyrreshape2

library(dplyr)
library(reshape2)
dat=dat%>%group_by(V1)%>%dplyr::mutate(id=row_number())
as.data.frame(acast(dat, V1~id,value.var="V2"))

  1 2 3
a 1 4 2
b 6 8 4
c 4 5 2

数据输入 :

dat
  V1 V2
1  a  1
2  a  4
3  a  2
4  b  6
5  b  8
6  b  4
7  c  4
8  c  5
9  c  2

编辑:时间

library(microbenchmark)
microbenchmark(
    acastmethod=acast(dat, a~id,value.var="b"), 
    dcastmethod=dcast(dat, a ~ id , value.var = "b"),
    tidyrmethod=spread(dat, key = id, value = b),
    xtabmethod=xtabs(b ~ a + id, data = dat)

)


Unit: milliseconds
        expr      min       lq     mean   median       uq       max neval  cld
 acastmethod 1.872223 2.035528 2.237846 2.210701 2.349068  3.783507   100 a   
 dcastmethod 3.124578 3.405817 3.626199 3.579038 3.815807  4.887430   100  b  
 tidyrmethod 4.025684 4.477290 4.765803 4.725326 5.035862  6.140385   100   c 
  xtabmethod 5.054490 5.529382 6.378615 5.714020 6.047391 61.242200   100    d

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM