按行和列

时间:2016-02-04 23:06:37

标签: r transpose

如何在R中使用以下条件转置数据帧?在这种情况下,正常的转置不起作用。

Current data

Expected output

谢谢!

1 个答案:

答案 0 :(得分:1)

就像那样:

library(reshape2)
df <- data.frame(Name=c("Tom", "Tom", "Hardy", "Hardy"), 
                 Drug=c("drug1", "drug2", "drug1", "drug3"), 
                 Value=c(20,10,30,40))
dcast(df,Name~Drug, fill = 0 )

   Name drug1 drug2 drug3
1 Hardy    30     0    40
2   Tom    20    10     0