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

hi1*_*i15 5 r reshape reshape2 data.table

我有一个data.tableR

> dt
  SAMPLE   junction count
1: R1        a       1
2: R2        a       1
3: R3        b       1
4: R3        a       1
5: R1        c       2
Run Code Online (Sandbox Code Playgroud)

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

> m
      R1   R2   R3
  a    1    1    1
  b    0    0    1
  c    2    0    0
Run Code Online (Sandbox Code Playgroud)

akr*_*run 11

dcast来自data.table变化,从"长"到"宽"格式的数据集.

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
Run Code Online (Sandbox Code Playgroud)

如果需要矩阵输出

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
Run Code Online (Sandbox Code Playgroud)

xtabs来自base R

 xtabs(count~junction+SAMPLE, dt)
Run Code Online (Sandbox Code Playgroud)


Col*_*vel 5

用另一种方法spread来自tidyr:

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
Run Code Online (Sandbox Code Playgroud)

或者reshape来自的旧学校解决方案stats:

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
Run Code Online (Sandbox Code Playgroud)

数据:

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>)
Run Code Online (Sandbox Code Playgroud)