简体   繁体   English

如何将带有虚拟变量的矩阵(矩形)转换为带有数字的方阵?

[英]How to convert a matrix (rectangular) with dummy variables into a square matrix with numeric?

I have this situation, in which I have about 200.000 observations ("source") that follow 8 different "target".我有这种情况,我有大约 200.000 个观察值(“源”)遵循 8 个不同的“目标”。
If they follow that target is 1 otherwise is 0 like in the simplified example below:如果他们遵循该目标为 1,否则为 0,如下面的简化示例所示:

source来源 target1目标1 target2目标2 target3目标3
source1来源1 1 1个 0 0 1 1个
source2来源2 0 0 1 1个 1 1个
source3来源3 1 1个 1 1个 1 1个

Now, I want to know who follow more target and, consequently, how many times a same source follows more than one target, in other words, in each cell I want to know how many times both conditions are verified.现在,我想知道谁关注了更多的目标,因此,同一源关注了多个目标的次数,换句话说,在每个单元格中,我想知道两个条件都得到了多少次验证。
This would be the idea:这将是这样的想法:

(blank) (空白的) target1目标1 target2目标2 target3目标3
target1目标1 2 2个 1 1个 2 2个
target2目标2 1 1个 2 2个 2 2个
target3目标3 2 2个 2 2个 3 3个
library(dplyr)
dat <- tibble::tribble(
  ~source,  ~target1,   ~target2,   ~target3,
"source1",  1,  0,  1,
"source2",  0,  1,  1,
"source3",  1,  1,  1)

mat <- dat %>% select(-source) %>% as.matrix()
crossprod(mat)
#>         target1 target2 target3
#> target1       2       1       2
#> target2       1       2       2
#> target3       2       2       3

Created on 2022-11-27 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-11-27

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM