从伪编码观察中创建共生矩阵

时间:2012-05-16 16:27:52

标签: r

是否有一种简单的方法可以将带有虚拟对象的数据框转换为是否存在方面,以及包含两个方面共同计数的共生矩阵?

E.g。从这个开始

X <- data.frame(rbind(c(1,0,1,0), c(0,1,1,0), c(0,1,1,1), c(0,0,1,0)))
X
  X1 X2 X3 X4
1  1  0  1  0
2  0  1  1  0
3  0  1  1  1
4  0  0  1  0

到这个

   X1 X2 X3 X4
X1  0  0  1  0
X2  0  0  2  1
X3  1  2  0  1
X4  0  1  1  0

2 个答案:

答案 0 :(得分:13)

这样可以解决问题:

X <- as.matrix(X)
out <- crossprod(X)  # Same as: t(X) %*% X
diag(out) <- 0       # (b/c you don't count co-occurrences of an aspect with itself)
out
#      [,1] [,2] [,3] [,4]
# [1,]    0    0    1    0
# [2,]    0    0    2    1
# [3,]    1    2    0    1
# [4,]    0    1    1    0

要将结果与您展示的结果完全相同,您可以执行以下操作:

nms <- paste("X", 1:4, sep="")
dimnames(out) <- list(nms, nms)
out <- as.data.frame(out)

答案 1 :(得分:0)

虽然没有什么能比得上上述答案的简单性,只是发布 tidyverse 方法以供将来参考

Y <- X %>% mutate(id = row_number()) %>%
  pivot_longer(-id) %>% filter(value !=0)

merge(Y, Y, by = "id", all = T) %>%
  filter(name.x != name.y) %>%
  group_by(name.x, name.y) %>%
  summarise(val = n()) %>%
  pivot_wider(names_from = name.y, values_from = val, values_fill = 0, names_sort = T) %>%
  column_to_rownames("name.x")

   X1 X2 X3 X4
X1  0  0  1  0
X2  0  0  2  1
X3  1  2  0  1
X4  0  1  1  0