R中列的NxN制表,并检查列之间的重复性

问题描述 投票:1回答:1

我有一个数据框,在行上采购了唯一的项目,在列上采购了它们的相应来源(基本上这是我从原始数据中“抛弃”的数据)

现在看起来像这样

DF

现在基于此广播数据,我必须创建一个表来检查来自此类来源的重复项

This is the desired output

r dplyr tidyverse tidyr dcast
1个回答
1
投票

您可以通过以下代码获取它:

m <- as.matrix(DF[-1])
z <- t(m)%*%m
diag(z) <- NA

给出:

> z
        Source1 Source2 Source3 Source4
Source1      NA       2       0       1
Source2       2      NA       2       0
Source3       0       2      NA       0
Source4       1       0       0      NA
© www.soinside.com 2019 - 2024. All rights reserved.