Something like expand.grid on a list of lists

前端 未结 3 924
花落未央
花落未央 2021-02-13 04:16

I have three text documents stored as a list of lists called \"dlist\":

dlist <- structure(list(name = c(\"a\", \"b\", \"c\"), text = list(c(\"the\", \"quick\         


        
3条回答
  •  暖寄归人
    2021-02-13 04:48

    Josh's answer is much sweeter but I thought I'd throw my hat in the ring.

    dlist <- structure(list(name = c("a", "b", "c"), 
        text = list(c("the", "quick", "brown"), 
        c("fox", "jumps", "over", "the"), c("lazy", "dog"))), 
        .Names = c("name", "text"))
    
    lens <- sapply(unlist(dlist[-1], recursive = FALSE), length)
    
    data.frame(name = rep(dlist[[1]], lens), text = unlist(dlist[-1]), row.names = NULL)
    
    ##   name  text
    ## 1    a   the
    ## 2    a quick
    ## 3    a brown
    ## 4    b   fox
    ## 5    b jumps
    ## 6    b  over
    ## 7    b   the
    ## 8    c  lazy
    ## 9    c   dog
    

    That being said the list of lists is a bit of an awkward storage method. A list of vectors (particularly named lists of vectors) would be easier to deal with.

提交回复
热议问题