小编jac*_*tra的帖子

创建共生矩阵

我正在尝试解决共同出现矩阵的问题.我有一个事务和项目的数据文件,我想看到一起显示项目的事务数量的矩阵.

我是R编程的新手,我很乐意找到R所有的快捷方式,而不是创建特定的循环(我以前使用的是C年前,现在只坚持使用Excel宏和SPSS).我已经检查过这里的解决方案,但是没有找到一个有效的方法(最接近的是这里给出的解决方案:使用SAC的共生矩阵? - 但是当我使用projection_tm时它产生了一条错误信息,我怀疑cbind不是'在我的案例中成功.

基本上我有一个包含以下内容的表:

TrxID Items Quant
Trx1 A 3
Trx1 B 1
Trx1 C 1
Trx2 E 3
Trx2 B 1
Trx3 B 1
Trx3 C 4
Trx4 D 1
Trx4 E 1
Trx4 A 1
Trx5 F 5
Trx5 B 3
Trx5 C 2
Trx5 D 1, etc.
Run Code Online (Sandbox Code Playgroud)

我想创建类似的东西:

   A B C D E F
A  0 1 1 0 1 1
B  1 0 3 1 1 0
C  1 3 0 1 0 0 …
Run Code Online (Sandbox Code Playgroud)

r matrix

28
推荐指数
5
解决办法
2万
查看次数

将变量组合到一个列表中

伙计们,

我很难接受以下挑战.我有一个如下所示的数据集:

BuyerID    Fruit.1     Fruit.2    Fruit.3    Amount.1    Amount.2    Amount.3
879        Banana      Apple                 4           3
765        Strawberry  Apple      Orange     1           2           4
123        Orange      Banana                1           1           1
 11        Strawberry                        3
773        Kiwi        Banana                1           2
Run Code Online (Sandbox Code Playgroud)

我想做的是简化数据(如果可能)并折叠"Fruit"和"Amount"变量

BuyerID    Fruit                             Amount      Total    Count
879        "Banana" "Apple"                  4  3            7        2
765        "Strawberry" "Apple" "Orange"     1  2  4         7        3
123        "Orange" "Banana"                 1  1  1         3        2
 11        "Strawberry"                      3               3        1
773        "Kiwi" "Banana"                   1  2            3        2
Run Code Online (Sandbox Code Playgroud)

我已经尝试过使用c()和rbind(),但是它们没有产生我想要的结果 - 我在这里尝试了一些提示: …

variables r list dataframe

7
推荐指数
3
解决办法
1055
查看次数

标签 统计

r ×2

dataframe ×1

list ×1

matrix ×1

variables ×1