将不规则的嵌套列表转换为数据框

时间:2019-03-20 16:30:14

标签: r list dataframe

我有一个嵌套列表,如下所示:

library(data.table)
setDT(xy)
xy[, rleid(val), id]

   id V1
1:  1  1
2:  1  1
3:  1  2
4:  1  3
5:  1  3
6:  2  1
7:  2  2

我想将其转换为一个数据帧,其中每个子条目与所有父条目并排。所以我最终得到一个看起来像

的数据框
    mylist <- list(
      list(
        id = 1234,
        attributes = list(
             list(
               typeId = 11,
               type = 'Main',
               date = '2018-01-01', 
               attributes= list(
                 list(
                   team = 'team1',
                   values = list(
                     value1 = 1, 
                     value2 = 999)),
                 list(
                   team = 'team2',
                   values = list(
                     value1 = 2, 
                     value2 = 888))
                 )
               ),
             list(
               typeId = 12,
               type = 'Extra',
               date = '2018-01-02', 
               attributes= list(
                 list(
                   team = 'team1',
                   values = list(
                     value1 = 3, 
                     value2 = 1234)),
                 list(
                   team = 'team2',
                   values = list(
                     value1 = 4, 
                     value2 = 9876))
               )
             )
          )
        )
      )

我并不总是知道列表中的名称,因此需要一种通用的方式来执行此操作而无需指定列名

编辑

我对最初的问题有一个答案,但是针对Parfaits的评论:“如果您发布原始JSON和R导入代码,则可能会有更简单的解决方案。”

我可以使用R代码从网址中获取原始JSON:

    id type_id  type       date  team value1 value2
1 1234      11  Main 2018-08-01 team1      1    999
2 1234      11  Main 2018-08-01 team2      2    888
3 1234      12 Extra 2018-08-02 team1      3   1234
4 1234      12 Extra 2018-08-02 team2      4   9876

在url中,JSON如下所示:

httr::GET( feed_url, authenticate(username, password) ) %>% httr::content()

1 个答案:

答案 0 :(得分:1)

现在具有执行此操作的功能:

flattenList <- function(input) {

    output <- NULL

    ## Check which elements of the current list are also lists.
    isList <- sapply(input, class) == "list"

    ## Any non-list elements are added to the output data frame.
    if (any(!isList)) {

        ## Determine the number of rows in the output.
        maxRows <- max(sapply(input[!isList], length))

        output <-
            ## Initialise the output data frame with a dummy variable.
            data.frame(dummy = rep(NA, maxRows)) %>%

            ## Append the new columns.
            add_column(!!! input[!isList]) %>%

            ## Delete the dummy variable.
            select(- dummy)
    }

    ## If some elemenets of the current list are also lists, we apply the function again.
    if (any(isList)) {

        ## Apply the function to every sub-list, then bind the new output as rows.
        newOutput <- lapply(input[isList], flattenList) %>% bind_rows()

        ## Check if the current output is NULL.
        if (is.null(output)) {

            output <- newOutput

        } else {

            ## If the current output has fewer rows than the new output, we recycle it.
            if (nrow(output) < nrow(newOutput)) {
                output <- slice(output, rep(1:n(), times = nrow(newOutput) / n()))
            }


            ## Append the columns of the new output.
            output <- add_column(output, !!! newOutput)
        }
    }

    return(output)
}

> flattenList(mylist)
    id typeId  type       date  team priority value1 value2
1 1234     11  Main 2018-01-01 team1        1      1    999
2 1234     11  Main 2018-01-01 team2        1      2    888
3 1234     12 Extra 2018-01-02 team1        1      3   1234
4 1234     12 Extra 2018-01-02 team2        1      4   9876