【问题标题】:I want to modify the data and fill the values of the modified matrix by frequency in R [duplicate]我想修改数据并按R中的频率填充修改后矩阵的值[重复]
【发布时间】:2017-09-21 06:26:13
【问题描述】:

我有一个如下的数据框

    Users          Products
     101           Potassium Monosulfame 
     102           Kathon
     103           Tenox
     102           Potassium Monosulfame
     101           Tenox
     101           Potassium Monosulfame
     103           Kathon 
     101           Potassium Monosulfame
     103           Kathon

我想将此数据转换为一个矩阵,其中用户为行,每个唯一产品为列,值作为频率。所需的输出如下所示

   Users         Potassium Monosulfame    Kathon      Tenox
    101                   3                  0          1
    102                   1                  1          0
    103                   0                  2          1

请指导我如何在 R 中获得此输出。

【问题讨论】:

  • table(df$Users, df$Products)

标签: r


【解决方案1】:
d <- data.frame(
Users=c( '101', '102', '103', '102', '101', '101', '103', '101', '103' ),
Products=c( 'Potassium Monosulfame', 'Kathon', 'Tenox', 'Potassium Monosulfame', 'Tenox', 'Potassium Monosulfame', 'Kathon', 'Potassium Monosulfame', 'Kathon' ))
library(reshape2)
dcast(data = d,formula = Users~Products)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-07
    • 2020-03-03
    • 2021-03-12
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    相关资源
    最近更新 更多