data.table到嵌套列表中

时间:2016-12-06 16:14:16

标签: r list data.table nested-lists

我有一个data.table如下,

## install.packages(c("gapminder", "data.table"))
library(gapminder)
library(data.table)
gapminder <- data.table(gapminder)
my_table <- gapminder[, .(mdl = .(lm(lifeExp ~ pop + gdpPercap, 
                                  data = gapminder))), 
                          by = .(country, continent)]

结果表将是,

                country continent  mdl
  1:        Afghanistan      Asia <lm>
  2:            Albania    Europe <lm>
  3:            Algeria    Africa <lm>
  4:             Angola    Africa <lm>
  5:          Argentina  Americas <lm>
 ---                                  
138:            Vietnam      Asia <lm>
139: West Bank and Gaza      Asia <lm>
140:        Yemen, Rep.      Asia <lm>
141:             Zambia    Africa <lm>
142:           Zimbabwe    Africa <lm>

现在我想从这个data.table中获取一个列表,mdl应位于每个country内,continent本身嵌套在first_list <- split(my_table, my_table$continent) second_list <- lapply(first_list, function(x){ split(x[, country := as.character(country)], x$country) }) final_list <- sapply(second_list, function(x) sapply(x, function(y) y$mdl)) 中。

我试图将结果作为,

function postTransactionToGl() {
                            var invoiceId = $("input[id='showGlTransactions_Header_invoiceId_id']").val();

                            $.ajax(
                                {
                                    type:"POST",
                                    url:"${sri.buildUrl('postInvoiceToGl').url}",
                                    data:
                                        {
                                            moquiSessionToken: "${(ec.getWeb().sessionToken)!}",
                                            invoiceId: invoiceId
                                        },
                                    dataType:"json"
                                }
                            );
                        };

有没有优雅的方法来做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以使用此类代码获取您正在寻找的嵌套列表:

    res<-lapply(unique(my_table$continent),
function(x){lapply(unique(my_table[continent==x]$country),
function(z){my_table[continent==x&country==z]})})

答案 1 :(得分:1)

您可以使用data.tree包:

library(data.tree)
# create a path string
my_table$pathString <- paste("world", my_table$continent, my_table$country, sep = "/")

# convert the data.table to nodes and nested lists
nested_list <- as.list(as.Node(my_table[, .(pathString, mdl)]))

# query the result
nested_list[["Asia"]][["Vietnam"]]

#$mdl
#$mdl[[1]]

#Call:
#lm(formula = lifeExp ~ pop + gdpPercap, data = gapminder)

#Coefficients:
#(Intercept)          pop    gdpPercap  
#  5.365e+01    9.728e-09    7.676e-04  

或另一种选择:

nested_list <- lapply(split(my_table, by = "continent"), 
                      function(dt) setNames(dt$mdl, dt$country))

nested_list[["Asia"]][["Vietnam"]]

#Call:
#lm(formula = lifeExp ~ pop + gdpPercap, data = gapminder)

#Coefficients:
#(Intercept)          pop    gdpPercap  
#  5.365e+01    9.728e-09    7.676e-04