R-将数据转换为与其他矩阵相同的矩阵格式
我在excel中的数据如下:R-将数据转换为与其他矩阵相同的矩阵格式,r,matrix,R,Matrix,我在excel中的数据如下: Terms Category Weight email TV 1.00 acccount Email 12.0 accept Phone 3.00 我有其他矩阵,其格式为: Terms TV Email Phone Contact Information Support ..... achiev
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的程序完成这项工作。任何帮助都将不胜感激。提前感谢。您需要重塑数据。如果您还没有“重塑2”软件包,请安装该软件包 下面是重塑数据的代码
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)
require(重塑2)
你不能提供一些数据吗?是的,读这本书会很有用。。但是怎么做呢?@Paulo Cardoso你不能按“术语”合并并得到结果。在dcast
(+1)很好地使用了melt
和dcast
谢谢大家。。得到我想要的。非常感谢:)