How to create an adjacency matrix from raw data which is non-numeric in nature [duplicate]

StackOverflow https://stackoverflow.com/questions/17525231

  •  02-06-2022
  •  | 
  •  

Question

An example of the input I am working on is given below:

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

The above data is a record of music listened to by users of an app.

I would like to generate an adjacency matrix corresponding to the below given example:

           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

How would this be possible in R. Any tips or pointers would be most appreciated.

Thank you in advance for your time and help.

Was it helpful?

Solution 2

This works:

# 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

OTHER TIPS

If DF is the two column data frame corresponding to the data in the question then:

xtabs(data = DF)

which gives:

           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

Note: We used this for the input:

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))

The qdap package has the adjmat function that can do this:

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 Nice approach to reading in the data :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top