Flatten list column in data frame with ID column

前端 未结 4 564
逝去的感伤
逝去的感伤 2021-01-17 11:16

My data frame contains the output of a survey with a select multiple question type. Some cells have multiple values.

df <- data.frame(a=1:3,b=I(list(1,1:2         


        
4条回答
  •  庸人自扰
    2021-01-17 11:51

    Here's one way, with data.table:

    require(data.table)
    data.table(df)[,as.integer(unlist(b)),by=a]
    

    If b is stored consistently, as.integer can be skipped. You can check with

    unique(sapply(df$b,class))
    # [1] "numeric" "integer"
    

提交回复
热议问题