In R write list with several elements to data.frame

后端 未结 4 357
自闭症患者
自闭症患者 2021-01-29 05:51

I have a named list whose each element is a character vector. I want to write this list into a single dataframe where I have two columns, one with the name of the character vect

4条回答
  •  孤独总比滥情好
    2021-01-29 06:19

    A very straightforward way would be to use cbind(), like this:

    cbind(names(l),l)
    

    This will result in the following data frame, assuming that l = list(a="ax", b="bx"):

          l   
    a "a" "ax"
    b "b" "bx"
    

    Of course, you can rename the columns and rows by adjusting the values in colnames(l) and rownames(l). In this example, the string names are automatically also applied to the rownames of the resulting data frame, so, depending on what you'd like to do with your data,

    cbind(l)
    

    might suffice, resulting in

      l   
    a "ax"
    b "bx"
    

    Hope I could help.

提交回复
热议问题