How to flatten a list of lists?

后端 未结 3 1527
予麋鹿
予麋鹿 2020-11-28 06:07

The tm package extends c so that, if given a set of PlainTextDocuments it automatically creates a Corpus. Unfortunately,

相关标签:
3条回答
  • 2020-11-28 06:25

    Here's another method that worked for my list of lists.

    df <- as.data.frame(do.call(rbind, lapply(foolist, as.data.frame)))

    0 讨论(0)
  • 2020-11-28 06:42

    I expect that unlist(foolist) will help you. It has an option recursive which is TRUE by default.

    So unlist(foolist, recursive = FALSE) will return the list of the documents, and then you can combine them by:

    do.call(c, unlist(foolist, recursive=FALSE))
    

    do.call just applies the function c to the elements of the obtained list

    0 讨论(0)
  • 2020-11-28 06:44

    Here's a more general solution for when lists are nested multiple times and the amount of nesting differs between elements of the lists:

     flattenlist <- function(x){  
      morelists <- sapply(x, function(xprime) class(xprime)[1]=="list")
      out <- c(x[!morelists], unlist(x[morelists], recursive=FALSE))
      if(sum(morelists)){ 
        Recall(out)
      }else{
        return(out)
      }
    }
    
    0 讨论(0)
提交回复
热议问题