简体   繁体   中英

How to convert a list to a data.frame in R

I want to convert a list to a data frame in R.

My list:

myList <- list(
`JJ-NN-NN` = c("hybridized","cmp","conditioner"),
`JJ-JJ-NN` = c("first", "abrasive", "unit"))

> myList
$`JJ-NN-NN`
[1] "hybridized"  "cmp"         "conditioner"
$`JJ-JJ-NN`
[1] "first"    "abrasive" "unit"   

I want to get this data frame.

    POS          Word
1   JJ-NN-NN     hybridized cmp conditioner
2   JJ-JJ-NN     first abrasive unit

please try the code below:

   a=do.call(rbind,lapply(myList,paste0,collapse=" "))
    data.frame(pos=rownames(a),word=a,row.names = NULL)

If this helps please let us know. Thank you

EDIT:

aggregate(.~ind,stack(myList),paste,collapse=' ')
       ind                     values
1 JJ-NN-NN hybridized cmp conditioner
2 JJ-JJ-NN        first abrasive unit

Here is another solution that stays within the tidyverse .

library(tidyverse)
map(myList, paste0, collapse = " ") %>% bind_rows() %>% gather(POS, Word)

也许更直接:

data.frame(POS = names(myList),Word = sapply(myList,paste,collapse=" "),row.names = seq_along(myList))

Hello all ;

This can be helpful

myList<- myData

myData<-data.frame(myData)

If you want to stack your variables for ANOVA purpose then use this

myData<-stack(myData)

Hope it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM