[英]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
dcast
的data.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
或與老同學的解決方案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
數據:
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.