Unlisting columns by groups

后端 未结 4 1765
天命终不由人
天命终不由人 2020-12-03 15:55

I have a dataframe in the following format:

id | name               | logs                                  
---+--------------------+-----------------------         


        
相关标签:
4条回答
  • 2020-12-03 16:31

    This is a perfect case for tidyr:

    library(tidyr)
    library(dplyr)
    dat %>% unnest(logs)
    
    0 讨论(0)
  • 2020-12-03 16:31

    Another data.table option

    library(data.table)
    dt <- data.table(df)
    dt[,.(id,logs=logs[[1]]), by = name]
    
    0 讨论(0)
  • 2020-12-03 16:36

    Using listCol_l from splitstackshape could be a good option here as the column "logs" in the data.frame is a list

    library(splitstackshape)
    listCol_l(df, 'logs')
    
     #    id           name   logs_ul
     #1: 148        avihil1 Z47331572
     #2: 149         Niarfe Z47031573
     #3: 150 doug henderson F47531574
     #4: 150 doug henderson   B195945
     #5: 150 doug henderson   D186871
     #6: 150 doug henderson   S192939
     #7: 150 doug henderson   S182865
     #8: 150 doug henderson G19539045
     #9: 151       nick tan A47231575
    #10: 151       nick tan   A190933
    #11: 151       nick tan   C181859
    #12: 152         madisp F47431576
    #13: 153       woodbusy B47231577
    #14: 153       woodbusy   D193936
    #15: 153       woodbusy   Q184862
    #16: 154    kevinhcross Y47331579
    #17: 155          cylol A47531580
    #18: 155          cylol   Z195944
    #19: 155          cylol   B185870
    #20: 156    andrewarrow N47731581
    #21: 157       gstavrev E47231582
    
    0 讨论(0)
  • 2020-12-03 16:39

    Just to show another option

    library(data.table)
    setDT(df)[, .(logs = unlist(logs)), by = .(id, name)]
    
    0 讨论(0)
提交回复
热议问题