Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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_Matrix - Fatal编程技术网

R-将数据转换为与其他矩阵相同的矩阵格式

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

我在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的程序完成这项工作。任何帮助都将不胜感激。提前感谢。

您需要重塑数据。如果您还没有“重塑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
谢谢大家。。得到我想要的。非常感谢:)