Find all sequences with the same column value

后端 未结 9 944
误落风尘
误落风尘 2020-12-17 18:35

I have the following data frame:

╔══════╦═════════╗
║ Code ║ Airline ║
╠══════╬═════════╣
║    1 ║ AF      ║
║    1 ║ KL      ║
║    8 ║ AR      ║
║    8 ║ A         


        
9条回答
  •  庸人自扰
    2020-12-17 19:02

    Take the following as a comment that is posted as an answer just because this allows more convenient formatting.

    for each code
      lookup all rows in the table where the value = code
    

    ummm... sorry, I don't get how this psedudocode is related to your desired output

    +--------------------+
    | Airline SharedWith |
    +--------------------+
    | AF      "KL"       |
    | KL      "AF"       |
    | AR      "AZ","DL"  |
    +--------------------+
    

    The result of this pseudocode should rather be:

    +---------------------+
    + Code  +  Airlines   +
    +---------------------+
    +  1    +  AF, KL     +
    +  2    +  AR, AZ, DL +
    +---------------------+
    

    That is,

    codes <- unique(dat$Code)
    data.frame(Code=codes, Airlines = sapply(codes, function(x) paste(subset(dat, Code %in% x)$Airline, collapse=",")))
    

提交回复
热议问题