[英]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![]() |
target2![]() |
target3![]() |
---|---|---|---|
source1![]() |
1 ![]() |
0 ![]() |
1 ![]() |
source2![]() |
0 ![]() |
1 ![]() |
1 ![]() |
source3![]() |
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![]() |
target2![]() |
target3![]() |
---|---|---|---|
target1![]() |
2 ![]() |
1 ![]() |
2 ![]() |
target2![]() |
1 ![]() |
2 ![]() |
2 ![]() |
target3![]() |
2 ![]() |
2 ![]() |
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.