我有以下数据框:
class outcome count total
A TP 5 20
A FP 5 20
A TN 5 20
A FN 5 20
B TP 10 40
B FP 10 40
B TN 10 40
B FN 10 40
我基本上想要这种所谓的宽格式,即
type TP FP TN FN total
A 5 5 5 5 20
B 10 10 10 10 40
我几乎可以通过这样做:
> dcast(test,test$outcome ~ test$class)
Using class...outcome...count....total as value column: use value.var to override.
. A FN 5 20 A FP 5 20 A TN 5 20 A TP 5 20
1 . A FN 5 20 A FP 5 20 A TN 5 20 A TP 5 20
B FN 10 40 B FP 10 40 B TN 10 40 B TP 10 40
1 B FN 10 40 B FP 10 40 B TN 10 40 B TP 10 40
我现在每个结果类型都有一个列,但我缺少列名,重复行和我想要的列标题(TP,FP,TN,FN)作为列值...仍然。
所以也相当遥远。
dcast甚至可以实现这一点吗?
答案 0 :(得分:3)
以下是tidyr
使用spread
如何做到这一点:
library(tidyr)
df1 <-read.table(text="class outcome count total
A TP 5 20
A FP 5 20
A TN 5 20
A FN 5 20
B TP 10 40
B FP 10 40
B TN 10 40
B FN 10 40",header=TRUE, stringsAsFactors=FALSE)
library(tidyr)
spread(df1,outcome,count)
class total FN FP TN TP
1 A 20 5 5 5 5
2 B 40 10 10 10 10
这是dcast
解决方案:
dcast(df1, class +total ~ outcome, value.var="count")
class total FN FP TN TP
1 A 20 5 5 5 5
2 B 40 10 10 10 10
答案 1 :(得分:0)
在基础R中使用reshape
(其中df
是您的数据框)
reshape(df, idvar = c("class", "total"), timevar = "outcome", direction = "wide")
# class total count.TP count.FP count.TN count.FN
#1 A 20 5 5 5 5
#5 B 40 10 10 10 10