根据需要将R数据帧转换为嵌套列表

问题描述 投票:0回答:1

我有一个数据框如下:

df <- data.frame(
      all = c("All product","All product","All product","All product","All product","All product","All product","All product",
              "All product","All product","All product","All product","All product","All product","All product","All product"),
      category = c("Agriculture","Agriculture","Agriculture","Agriculture", "Agriculture","Agriculture","Agriculture","Agriculture",
                   "Non Agriculture", "Non Agriculture", "Non Agriculture", "Non Agriculture","Non Agriculture", "Non Agriculture", "Non Agriculture", "Non Agriculture"),
      HS2 = c("01","01","01","01","02","02","02","02","03","03","03","03","04","04","04","04"),
      HS4 = c("0101","0101","0102","0102","0201","0201","0202","0202","0301","0301","0302","0302","0401","0401","0402","0402"),
      HS6 = c("010101","010102", "010201","010202","020101","020102","020201","020202","030101","030102", "030201","030202","040101","040102","040201","040202")
    )

我希望能够在嵌套列表中转换此数据框,如下所示:

 nodes <- list(
      list(
        text = "All product",
        type = "root",
        children = list(
          list(
            text = "Agriculture",
            type = "child",
            children= list( 
              list(
                text = "01",
                type = "child",
                children= list( 
                  list(
                    text = "0101",
                    type = "child",
                    children= list( 
                      list(
                        text = "010101",
                        type = "child"
                        ),
                      list(
                        text = "010102",
                        type = "child"
                      )
                      )
                    ),
                  list(
                    text = "0102",
                    type = "child",
                    children= list( 
                      list(
                        text = "010201",
                        type = "child"
                      ),
                      list(
                        text = "010202",
                        type = "child"
                      )
                    )
                  )
                  )
                
                ),
              list(
                text = "02",
                type = "child",
                children= list( 
                  list(
                    text = "0201",
                    type = "child",
                    children= list( 
                      list(
                        text = "020101",
                        type = "child"
                      ),
                      list(
                        text = "020102",
                        type = "child"
                      )
                    )
                  ),
                  list(
                    text = "0202",
                    type = "child",
                    children= list( 
                      list(
                        text = "020201",
                        type = "child"
                      ),
                      list(
                        text = "020202",
                        type = "child"
                      )
                    )
                  )
                )
                
              )
              )
          ),
          list(
            text = "Non Agriculture",
            type = "child",
            children= list( 
              list(
                text = "03",
                type = "child",
                children= list( 
                  list(
                    text = "0301",
                    type = "child",
                    children= list( 
                      list(
                        text = "030101",
                        type = "child"
                      ),
                      list(
                        text = "030102",
                        type = "child"
                      )
                    )
                  ),
                  list(
                    text = "0302",
                    type = "child",
                    children= list( 
                      list(
                        text = "030201",
                        type = "child"
                      ),
                      list(
                        text = "030202",
                        type = "child"
                      )
                    )
                  )
                )
                
              ),
              list(
                text = "04",
                type = "child",
                children= list( 
                  list(
                    text = "0401",
                    type = "child",
                    children= list( 
                      list(
                        text = "040101",
                        type = "child"
                      ),
                      list(
                        text = "040102",
                        type = "child"
                      )
                    )
                  ),
                  list(
                    text = "0402",
                    type = "child",
                    children= list( 
                      list(
                        text = "040201",
                        type = "child"
                      ),
                      list(
                        text = "040202",
                        type = "child"
                      )
                    )
                  )
                )
                
              )
            )
          )
        )
      )
    )

我已经手动编写了该列表。然而,我的情况是,我将拥有一个如上所述的数据框,并且数据框数据可以随时更改,但结构不能更改。如何将数据框转换为我共享的嵌套列表。

r dataframe list nested-lists
1个回答
0
投票

这里有一个方法。我从

jsTreeR
包中给出的示例中获取了 makeNodes 函数。



# make the nodes list from a vector of file paths
makeNodes <- function(leaves){
  dfs <- lapply(strsplit(leaves, "/"), function(s){
    item <-
      Reduce(function(a,b) paste0(a,"/",b), s[-1], s[1], accumulate = TRUE)
    data.frame(
      item = item,
      parent = c("root", item[-length(item)]),
      stringsAsFactors = FALSE
    )
  })
  dat <- dfs[[1]]
  for(i in 2:length(dfs)){
    dat <- merge(dat, dfs[[i]], all = TRUE)
  }
  f <- function(parent){
    i <- match(parent, dat$item)
    item <- dat$item[i]
    children <- dat$item[dat$parent==item]
    label <- tail(strsplit(item, "/")[[1]], 1)
    if(length(children)){
      list(
        text = label,
        children = lapply(children, f)
      )
    }else{
      list(text = label, type = "child")
    }
  }
  lapply(dat$item[dat$parent == "root"], f)
}

# the dataframe
df <- data.frame(
  all = c("All product","All product","All product","All product","All product","All product","All product","All product",
          "All product","All product","All product","All product","All product","All product","All product","All product"),
  category = c("Agriculture","Agriculture","Agriculture","Agriculture", "Agriculture","Agriculture","Agriculture","Agriculture",
               "Non Agriculture", "Non Agriculture", "Non Agriculture", "Non Agriculture","Non Agriculture", "Non Agriculture", "Non Agriculture", "Non Agriculture"),
  HS2 = c("01","01","01","01","02","02","02","02","03","03","03","03","04","04","04","04"),
  HS4 = c("0101","0101","0102","0102","0201","0201","0202","0202","0301","0301","0302","0302","0401","0401","0402","0402"),
  HS6 = c("010101","010102", "010201","010202","020101","020102","020201","020202","030101","030102", "030201","030202","040101","040102","040201","040202")
)

# transform it to a vector of paths
paths <- apply(df, 1, function(x) paste0(x, collapse = "/"))

# make the nodes list
nodes <- makeNodes(paths)

# we just have to add 'type = "root"' at the root
nodes[[1]]$type <- "root"

# let's visualize the result as JSON
jsonlite::toJSON(nodes, auto_unbox = TRUE, pretty = TRUE)
# [
#   {
#     "text": "All product",
#     "children": [
#       {
#         "text": "Agriculture",
#         "children": [
#           {
#             "text": "01",
#             "children": [
#               {
#                 "text": "0101",
#                 "children": [
#                   {
#                     "text": "010101",
#                     "type": "child"
#                   },
#                   {
#                     "text": "010102",
#                     "type": "child"
#                   }
#                 ]
#               },
#               {
#                 "text": "0102",
#                 "children": [
#                   {
#                     "text": "010201",
#                     "type": "child"
#                   },
#                   {
#                     "text": "010202",
#                     "type": "child"
#                   }
#                 ]
#               }
#             ]
#           },
#           {
#             "text": "02",
#             "children": [
#               {
#                 "text": "0201",
#                 "children": [
#                   {
#                     "text": "020101",
#                     "type": "child"
#                   },
#                   {
#                     "text": "020102",
#                     "type": "child"
#                   }
#                 ]
#               },
#               {
#                 "text": "0202",
#                 "children": [
#                   {
#                     "text": "020201",
#                     "type": "child"
#                   },
#                   {
#                     "text": "020202",
#                     "type": "child"
#                   }
#                 ]
#               }
#             ]
#           }
#         ]
#       },
#       {
#         "text": "Non Agriculture",
#         "children": [
#           {
#             "text": "03",
#             "children": [
#               {
#                 "text": "0301",
#                 "children": [
#                   {
#                     "text": "030101",
#                     "type": "child"
#                   },
#                   {
#                     "text": "030102",
#                     "type": "child"
#                   }
#                 ]
#               },
#               {
#                 "text": "0302",
#                 "children": [
#                   {
#                     "text": "030201",
#                     "type": "child"
#                   },
#                   {
#                     "text": "030202",
#                     "type": "child"
#                   }
#                 ]
#               }
#             ]
#           },
#           {
#             "text": "04",
#             "children": [
#               {
#                 "text": "0401",
#                 "children": [
#                   {
#                     "text": "040101",
#                     "type": "child"
#                   },
#                   {
#                     "text": "040102",
#                     "type": "child"
#                   }
#                 ]
#               },
#               {
#                 "text": "0402",
#                 "children": [
#                   {
#                     "text": "040201",
#                     "type": "child"
#                   },
#                   {
#                     "text": "040202",
#                     "type": "child"
#                   }
#                 ]
#               }
#             ]
#           }
#         ]
#       }
#     ],
#     "type": "root"
#   }
# ] 
© www.soinside.com 2019 - 2024. All rights reserved.