3

    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?

IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38
aastha
  • 45
  • 7
  • Your expected output doesnt make any sense to me. Many other names are not part of expected output. Moreover, `Sansa` appeared only once why it has count to self as `2`? – MKR Jul 14 '18 at 20:41
  • The expected output wasn't in line with the data information provided. The data provided was just a snipped of the entire data, just wanted to give out a rough idea of what I wanted at that time. Will make sure it's an accurate representation next time. – aastha Jul 21 '18 at 12:36

1 Answers1

2

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)
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
  • 1
    You could use `crossprod` `crossprod(with(DF, table(paste(seasonNum, sceneNum, sep="&"), character)))` ` – akrun Jul 14 '18 at 21:09
  • I updated that because I thought the OP wanted to have the first word – akrun Jul 14 '18 at 21:11