﻿ R共生矩阵水平数据 - 代码日志

R共生矩阵水平数据

R新手.我正在尝试使用水平数据创建共生矩阵.我想知道哪些元素在行中共同出现’TRUE’.

`````` dat <- read.table(text='"article"     "element1"   "element2"   "element3"   "element4"
1     "a house a home"     "TRUE"   "TRUE"   "FALSE"   "FALSE"
2     "cabin in the woods"     "TRUE"   "TRUE" "FALSE" "FALSE"
3     "motel is a hotel"   "TRUE"    "FALSE"   "TRUE"   "FALSE"', header=TRUE)
``````

``````# Make a vector of all elements.
elems <- colnames(dat)[-1]
# Make a sparse matrix
library(Matrix)
s <- Matrix(as.matrix(dat[elems]), sparse=TRUE, dimnames=list(dat\$article,elems))
# calculate co-occurrences
(t(s) %*% s)
# 4 x 4 sparse Matrix of class "dgCMatrix"
#          element1 element2 element3 element4
# element1        3        2        1        .
# element2        2        2        .        .
# element3        1        .        1        .
# element4        .        .        .        .

# If you don't want the exact number, and you want a "dense" matrix
as.matrix((t(s) %*% s) >= 1)
#          element1 element2 element3 element4
# element1     TRUE     TRUE     TRUE    FALSE
# element2     TRUE     TRUE    FALSE    FALSE
# element3     TRUE    FALSE     TRUE    FALSE
# element4    FALSE    FALSE    FALSE    FALSE
``````