繁体   English   中英

重塑数据表以使列名成为行名

[英]Reshaping data table to make column names into row names

我在R有一个data.table

> dt
  SAMPLE   junction count
1: R1        a       1
2: R2        a       1
3: R3        b       1
4: R3        a       1
5: R1        c       2

现在我想“重塑”数据表以形成data frame m (基本上通过样本矩阵连接,索引值为对应的计数值)。 另外,观察到对于dt不存在的(SAMPLE,junction)对,我假设相应的count数值zero 有人可以帮助我如何实现这一目标吗?

> m
      R1   R2   R3
  a    1    1    1
  b    0    0    1
  c    2    0    0

dcastdata.table将数据集从“long”格式更改为“wide”格式。

library(data.table)#v1.9.5+
dcast(dt, junction~SAMPLE, value.var='count', fill=0)
#   junction R1 R2 R3
#1:        a  1  1  1
#2:        b  0  0  1
#3:        c  2  0  0

如果需要矩阵输出

library(reshape2)
acast(dt, junction~SAMPLE, value.var='count', fill=0)
#   R1 R2 R3
#a  1  1  1
#b  0  0  1
#c  2  0  0

或者来自base R xtabs

 xtabs(count~junction+SAMPLE, dt)

使用tidyr spread的另一种方法:

library(tidyr)

spread(dt, SAMPLE, count, fill=0)
#   junction R1 R2 R3
#1:        a  1  1  1
#2:        b  0  0  1
#3:        c  2  0  0

或与老同学的解决方案reshapestats

reshape(dt, timevar='SAMPLE', idvar=c('junction'), direction='wide')
#   junction count.R1 count.R2 count.R3
#1:        a        1        1        1
#2:        b       NA       NA        1
#3:        c        2       NA       NA

数据:

dt = structure(list(SAMPLE = c("R1", "R2", "R3", "R3", "R1"), junction = c("a", 
"a", "b", "a", "c"), count = c(1, 1, 1, 1, 2)), .Names = c("SAMPLE", 
"junction", "count"), row.names = c(NA, -5L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x05e924a0>)

暂无
暂无

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

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