2

下面给出了我正在处理的输入示例:

User ID 1 --- Artist 5
User ID 2 --- Artist 1
User ID 3 --- Artist 7
User ID 4 --- Artist 2
User ID 5 --- Artist 3
User ID 1 --- Artist 2
User ID 3 --- Artist 1

上述数据是应用用户听过的音乐记录。

我想生成一个与以下给定示例相对应的邻接矩阵:

           ARTIST 1  ARTIST 2  ARTIST 3  ARTIST 4   ARTIST 5  ARTIST 6  ARTIST 7
USER ID 1     0        1         0          0         1         0         0
USER ID 2     1        0         0          0         0         0         0
USER ID 3     1        0         0          0         0         0         1
USER ID 4     0        1         0          0         0         0         0
USER ID 5     0        0         1          0         0         0         0

这在 R 中怎么可能实现。任何提示或指示将不胜感激。

提前感谢您的时间和帮助。

4

3 回答 3

4

如果DF是问题中数据对应的两列数据框则:

xtabs(data = DF)

这使:

           V2
V1          Artist 1 Artist 2 Artist 3 Artist 5 Artist 7
  User ID 1        0        1        0        1        0
  User ID 2        1        0        0        0        0
  User ID 3        1        0        0        0        1
  User ID 4        0        1        0        0        0
  User ID 5        0        0        1        0        0

注意:我们将其用于输入:

DF <- structure(list(V1 = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 3L), .Label = c("User ID 1", 
"User ID 2", "User ID 3", "User ID 4", "User ID 5"), class = "factor"), 
    V2 = structure(c(4L, 1L, 5L, 2L, 3L, 2L, 1L), .Label = c("Artist 1", 
    "Artist 2", "Artist 3", "Artist 5", "Artist 7"), class = "factor")), .Names = c("V1", 
"V2"), class = "data.frame", row.names = c(NA, -7L))
于 2013-07-08T13:33:15.353 回答
3

这有效:

# get data in useable form
ContingencyTable <- read.table(text=gsub(pattern = " --- ", replacement = ",","User ID 1 --- Artist 5
User ID 2 --- Artist 1
User ID 3 --- Artist 7
User ID 4 --- Artist 2
User ID 5 --- Artist 3
User ID 1 --- Artist 2
User ID 3 --- Artist 1"),sep=",", stringsAsFactors = FALSE)
# add variable for match value
ContingencyTable$Val <- 1
# more or less lifted from Arun's answer linked by @Hong Ooi, above
adjMat <- reshape2::dcast(ContingencyTable, V1 ~ V2, value.var = "Val", fill=0)
rownames(adjMat) <- adjMat[,1]
adjMat <- adjMat[,2:ncol(adjMat)]

adjMat
        Artist 1 Artist 2 Artist 3 Artist 5 Artist 7
User ID 1        0        1        0        1        0
User ID 2        1        0        0        0        0
User ID 3        1        0        0        0        1
User ID 4        0        1        0        0        0
User ID 5        0        0        1        0        0
于 2013-07-08T11:39:29.320 回答
2

qdap具有adjmat可以执行此操作的功能:

dat <- read.table(text=gsub(pattern = " --- ", replacement = ",",
"User ID 1 --- Artist 5
User ID 2 --- Artist 1
User ID 3 --- Artist 7
User ID 4 --- Artist 2
User ID 5 --- Artist 3
User ID 1 --- Artist 2
User ID 3 --- Artist 1"),sep=",", stringsAsFactors = FALSE)


library(qdap)
x <- with(dat, termco(V1, V2, unique(V1)))
adjmat(x)$boolean

## > adjmat(x)$boolean
##           Artist 1 Artist 2 Artist 3 Artist 5 Artist 7
## User ID 1        0        1        0        1        0
## User ID 2        1        0        0        0        0
## User ID 3        1        0        0        0        1
## User ID 4        0        1        0        0        0
## User ID 5        0        0        1        0        0

PS Tim Riffe 读取数据的好方法 :)

于 2013-07-08T13:12:56.763 回答