Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用列值创建宽格式表_R_Aggregate_Reshape - Fatal编程技术网

R 使用列值创建宽格式表

R 使用列值创建宽格式表,r,aggregate,reshape,R,Aggregate,Reshape,我有以下数据帧: 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 4

我有以下数据帧:

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甚至可以做到这一点吗?

以下是如何使用tidyr使用spread实现这一点:

在基R中使用重塑,其中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

这太棒了。这正是我想要的,也是我认为dcast应该做的。thaknks@brucezepplin我刚刚添加了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
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