简体   繁体   中英

Count occurrence of pair wise values in a column grouped by a unique combination of two other columns in r

  seasonNum character sceneNum 1: 1 Jon Snow 13 2: 1 Bran Stark 13 3: 1 Robb Stark 13 4: 1 Eddard Stark 13 5: 1 Rickon Stark 13 6: 1 Sansa Stark 13 7: 1 Arya Stark 13 8: 1 Eddard Stark 14 9: 1 Robb Stark 14 10: 1 Bran Stark 14 11: 1 Jon Snow 14 12: 1 Jon Snow 15 13: 1 Eddard Stark 15 14: 1 Bran Stark 15 15: 1 Robb Stark 15 16: 1 Eddard Stark 17 17: 1 Robb Stark 19 18: 1 Jon Snow 19 19: 1 Bran Stark 20 20: 1 Arya Stark 20 

structure(list(seasonNum = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), character = c("Jon Snow", 
"Bran Stark", "Robb Stark", "Eddard Stark", "Rickon Stark", "Sansa Stark", 
"Arya Stark", "Eddard Stark", "Robb Stark", "Bran Stark", "Jon Snow", 
"Jon Snow", "Eddard Stark", "Bran Stark", "Robb Stark", "Eddard Stark", 
"Robb Stark", "Jon Snow", "Bran Stark", "Arya Stark"), sceneNum = c(13L, 
13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 
15L, 17L, 19L, 19L, 20L, 20L)), .Names = c("seasonNum", "character", 
"sceneNum"), row.names = c(NA, -20L), class = "data.frame")

Thats the data I have I need it to be a nxn matrix of character names and the number of times they appeared together in a unique combination of seasonNum and SceneNum

  Arya Jon Sansa Arya 2 1 1 Jon 1 2 1 Sansa 1 1 2 

Can I use dcast function in R to achieve this?

I can't read in your data. But this is the logic. Let DF be your data table / frame.

LST <- with(DF, split(paste(seasonNum, sceneNum, sep = "&"), character) )
NUM_COMMON <- function (x, y) length(intersect(x, y))
outer(LST, LST, Vectorize(NUM_COMMON))

See


The cross tabular idea from akrun is another alternative.

XTAB <- with(DF, table(paste(seasonNum, sceneNum, sep="&"), character))
crossprod(XTAB)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM