从长到宽重塑2列data.table [英] Reshaping 2 column data.table from long to wide
本文介绍了从长到宽重塑2列data.table的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的data.frame:
This is my data.frame:
library(data.table)
df<- fread('
predictions Label
3 A
4 B
5 C
1 A
2 B
3 C
')
所需输出:
A B C
3 4 5
1 2 3
我正在尝试 DesiredOutput< -dcast(df,Label + predictions〜Label,value.var = predictions)
没有成功。感谢您的帮助!
I am trying DesiredOutput<-dcast(df, Label+predictions ~ Label, value.var = "predictions")
with no success. Your help is appreciated!
推荐答案
也许基本的R函数 unstack
是最干净的解决方案:
Maybe the base R function unstack
is the cleanest solution:
unstack(df)
A B C
1 3 4 5
2 1 2 3
请注意,这将返回data.frame而不是data.table,因此如果您想要最后一个data.table:
Note that this returns a data.frame rather than a data.table, so if you want a data.table at the end:
df2 <- setDT(unstack(df))
将返回一个数据表。
这篇关于从长到宽重塑2列data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文