Flatten list column in data frame with ID column

前端 未结 4 518
逝去的感伤
逝去的感伤 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:39

    Using base R, one option is stack after naming the list elements of 'b' column with that of the elements of 'a'. We can use setNames to change the names.

    stack(setNames(df$b, df$a))
    

    Or another option would be to use unstack to automatically name the list element of 'b' with 'a' elements and then do the stack to get a data.frame output.

    stack(unstack(df, b~a))
    

    Or we can use a convenient function listCol_l from splitstackshape to convert the list to data.frame.

    library(splitstackshape)
    listCol_l(df, 'b')
    
    0 讨论(0)
  • 2021-01-17 11:43

    You can just use unnest from "tidyr":

    library(tidyr)
    unnest(df, b)
    #   a b
    # 1 1 1
    # 2 2 1
    # 3 2 2
    # 4 3 1
    # 5 3 2
    # 6 3 3
    
    0 讨论(0)
  • 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"
    
    0 讨论(0)
  • 2021-01-17 11:52

    Here's another base solution, far less elegant than any other solution posted thus far. Posting for the sake of completeness, though personally I would recommend akrun's base solution.

    with(df, cbind(a = rep(a, sapply(b, length)), b = do.call(c, b)))
    

    This constructs the first column as the elements of a, where each is repeated to match the length of the corresponding list item from b. The second column is b "flattened" using do.call() with c().

    As Ananda Mahto pointed out in a comment, sapply(b, length) can be replaced with lengths(b) in the most recent version of R (3.2, if I'm not mistaken).

    0 讨论(0)
提交回复
热议问题