我在excel中有以下数据:
Terms Category Weight
email TV 1.00
acccount Email 12.0
accept Phone 3.00
我还有其他矩阵,其格式为:
Terms TV Email Phone Contact Information Support .....
achieve 1 0. 0 0 0 0
acquired 0 10.20 0 0 0 0
across 0 0 3.00 0 0 0
现在我想将上面的数据转换成上面的格式,例如
Terms TV Email Phone Contact Information Support .....
email 1 0. 0 0 0.0 0
acccount 0 12.0 0 0 0.0 0
accept 0 0 0 1.23 0 0
我想通过R中的程序来执行此操作。任何帮助将不胜感激。提前致谢。
您需要重塑数据。如果还没有安装软件包“ reshape2”
这是重塑数据的代码
require(reshape2)
df.reshape <-melt(df, id.var=c("Terms", "Category"))
#where df is your data.frame to be reshaped
#using both terms and category as ID variables
#now reshape it to wide format by casting
df.wide <-dcast(df.reshape, Terms~Category)
请注意,这将为您的数据中不存在的对提供NA。如果需要,可以轻松地将其替换为零
这是一个使用reshape2的不错的教程http://www.seananderson.ca/2013/10/19/reshape.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句