r - 如何使用R从具有多列的数据框中计算(共)发生矩阵?

标签 r dplyr igraph adjacency-matrix find-occurrences

我是 R 的新手,目前正在处理边缘列表形式的协作数据,该列表具有 32 列和大约 200.000 行。我想根据国家之间的相互作用创建一个(共)现矩阵。但是,我想通过对象的总数来计算交互次数。

期望结果的基本示例

如果在一行中“England”出现了 3 次而“China”只出现了一次,结果应该是下面的矩阵。

         England  China
England    3        3
China      3        1

可重现的例子
df <- data.frame(ID = c(1,2,3,4), 
 V1 = c("England", "England", "China", "England"),
 V2 = c("Greece", "England", "Greece", "England"),
V32 = c("USA", "China", "Greece", "England"))

因此,示例数据框当前看起来像这样:
ID  V1       V2       ...   V32
1   England  Greece         USA
2   England  England        China
3   China    Greece         Greece
4   England  England        England
.
.
.

期望的结果

我想逐行计算(共)出现并且与顺序无关,以获得一个(共)出现矩阵,该矩阵说明边缘循环(例如英格兰 - 英格兰)的低频,这导致以下结果:
         China   England   Greece   USA

China    2        2         2        0

England  2        6         1        1

Greece   2        1         3        1

USA      0        1         1        1

到目前为止已经尝试了什么

我用过 igraph得到一个共现的邻接矩阵。然而,它计算 - 正如预期的那样 - 不超过两个相同对象的交互,在某些情况下,我的值​​远低于按行/发布的对象的实际频率。
df <- data.frame(ID = c(1,2,3,4), 
 V1 = c("England", "England", "China", "England"),
 V2 = c("Greece", "England", "Greece", "England"),
V32 = c("USA", "China", "Greece", "England"))

# remove ID column

df[1] <- list(NULL)

# calculate co-occurrences and return as dataframe

library(igraph)
library(Matrix)

countrydf <- graph.data.frame(df)
countrydf2 <- as_adjacency_matrix(countrydf, type = "both", edges = FALSE)
countrydf3 <- as.data.frame(as.matrix(forceSymmetric(countrydf2)))

         China   England   Greece   USA

China    0        0         1        0

England  0        2         1        0

Greece   1        1         0        0

USA      0        0         0        0

我认为必须使用 base 有一个简单的解决方案和/或 dplyr和/或 table和/或 reshape2类似于 [1] , [2] , [3] , [4][5]但到目前为止,还没有解决任何问题,我无法根据自己的需要调整代码。我也试过使用 [6]然而,作为基础,同样的问题也适用于这里。
library(tidry)
library(dplyr)
library(stringr)


# collapse observations into one column

df2 <- df %>% unite(concat, V1:V32, sep = ",")

# calculate weights

df3 <- df2$concat %>%
  str_split(",") %>%
  lapply(function(x){
    expand.grid(x,x,x,x, w = length(x), stringsAsFactors = FALSE)
  }) %>%
  bind_rows

df4 <- apply(df3[, -5], 1, sort) %>%
  t %>%
  data.frame(stringsAsFactors = FALSE) %>%
  mutate(w = df3$w)

如果有人能指出我正确的方向,我会很高兴。

最佳答案

可能有更好的方法来做到这一点,但请尝试:

library(tidyverse)

df1 <- df %>%
pivot_longer(-ID, names_to = "Category", values_to = "Country") %>%
xtabs(~ID + Country, data = ., sparse = FALSE) %>% 
crossprod(., .) 

df_diag <- df %>% 
pivot_longer(-ID, names_to = "Category", values_to = "Country") %>%
mutate(Country2 = Country) %>%
xtabs(~Country + Country2, data = ., sparse = FALSE) %>% 
diag()

diag(df1) <- df_diag 

df1

Country   China England Greece USA
  China       2       2      2   0
  England     2       6      1   1
  Greece      2       1      3   1
  USA         0       1      1   1

关于r - 如何使用R从具有多列的数据框中计算(共)发生矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59632794/

相关文章:

arrays - 直接访问R中data.frame中的Vector元素?

在 dplyr 中重复 data.frame 的行

r - 使用igraph/R查找所有最短路径

r - 使用数据表或 igraph 按组查找直接邻居

rep 和/或 seq 函数来创建连续减少的向量?

r - 加速运行 if.. else 循环在 R

R:改进嵌套 ifelse 语句和多种模式

r - R-创建几列的非零最小值的新列

r - 将 data.frame 值更改为 %

c++ - 有没有办法检查在 C++ 中复制或分配对象是否安全?