Find all sequences with the same column value

后端 未结 9 805
误落风尘
误落风尘 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 18:52

    split helps. Here's a fully reproducible EDIT that works w/o any additional package. Works with the OPs data.frame - changed it after OP added a reproducible dataset.

    # strip white space in Airline names:
    dat$Airline <- gsub(" ","",dat$Airline)
    li <- split(dat,factor(dat$Code))
    do.call("rbind",lapply(li,function(x) 
    data.frame(Airline = x[1,2],
             SharedWith = paste(x$Airline[-1]
                                ,collapse=",")
    ))
    )
    

提交回复
热议问题