Something like expand.grid on a list of lists

前端 未结 3 922
花落未央
花落未央 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:43

    If you convert your dlist to a named list (a better suited structure in my opinion), you can use stack() to get the two column data.frame you want.

    (The rev() and setNames() calls in the second line are just one of many ways to adjust the column ordering and names to match the desired output shown in your question.)

    x <- setNames(dlist$text, dlist$name)
    setNames(rev(stack(x)),  c("name", "text"))
    #   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
    

提交回复
热议问题