How to count the number of occurrences of one variable that are shared between levels of another variable

前端 未结 1 1818
隐瞒了意图╮
隐瞒了意图╮ 2021-01-16 11:22

I have data that looks like

id      camera
angelina    cam03
angelina    cam03
angelina    cam03
angelina    cam03
angelina    cam03
angelina    cam03
angeli         


        
相关标签:
1条回答
  • 2021-01-16 11:45

    crossprod should do it for this:

    crossprod(table(mydf))
    #       Camera
    # Camera 0001 0002 0003
    #   0001    4    1    2
    #   0002    1    1    0
    #   0003    2    0    3
    

    diag can be used to set the diagonal to zero or NA if required. You can do it all in one go with:

    `diag<-`(crossprod(table(mydf)), 0)
    #       Camera
    # Camera 0001 0002 0003
    #   0001    0    1    2
    #   0002    1    0    0
    #   0003    2    0    0
    

    Sample data:

    mydf <- data.frame(
        Individual = c("A", "A", "B", "B", "C", "D", "E", "E"),
        Camera = c("0001", "0002", "0001", "0003", "0001", "0003", "0001", "0003"))
    

    EDIT:

    In case of duplicated cameras for same individual, you can eliminate the duplicates prior to the crossprod call:

    `diag<-`(crossprod(table(unique(mydf2))), 0)
           camera
    #camera  cam03 cam15 cam22 cam25
      #cam03     0     1     2     1
      #cam15     1     0     1     1
      #cam22     2     1     0     1
      #cam25     1     1     1     0
    

    Second data:

    mydf2 <- structure(list(id = c("angelina", "angelina", "angelina", "angelina", 
    "angelina", "angelina", "angelina", "angelina", "angelina", "angelina", 
    "angelina", "angelina", "barry", "barry", "barry", "barry", "barry", 
    "barry", "barry", "barry", "barry", "barry", "barry", "barry", 
    "barry", "barry", "barry", "barry", "barry"), camera = c("cam03", 
    "cam03", "cam03", "cam03", "cam03", "cam03", "cam03", "cam03", 
    "cam03", "cam03", "cam03", "cam22", "cam03", "cam03", "cam03", 
    "cam03", "cam03", "cam03", "cam03", "cam03", "cam03", "cam03", 
    "cam22", "cam22", "cam22", "cam22", "cam22", "cam15", "cam25"
    )), .Names = c("id", "camera"), class = "data.frame", row.names = c(NA, 
    -29L))
    
    0 讨论(0)
提交回复
热议问题