flatten a data frame

前端 未结 2 1286
独厮守ぢ
独厮守ぢ 2021-02-13 05:13

I have this nested data frame

test <- structure(list(id = c(13, 27), seq = structure(list(
`1` = c(\"1997\", \"1997\", \"1997\", \"2007\"),
`2` = c(\"2007\",         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-13 05:50

    This line does the trick:

    do.call("c", test[["seq"]])
    

    or equivalent:

    c(test[["seq"]], recursive = TRUE)
    

    or even:

    unlist(test[["seq"]])
    

    The output of these functions is:

        11     12     13     14     21     22     23     24     25     26     27 
    "1997" "1997" "1997" "2007" "2007" "2007" "2007" "2007" "2007" "2007" "2007" 
    

    To get rid of the names above the character vector, call as.character on the resulting object:

    > as.character((unlist(test[["seq"]])))
     [1] "1997" "1997" "1997" "2007" "2007" "2007" "2007" "2007" "2007" "2007"
    [11] "2007"
    

提交回复
热议问题