Data.table in a nested list

I have a data table as follows:

## 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)]

The resulting table will be,

                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>

Now I want to get a list from this data table that mdlshould lie inside each countryone that is itself embedded in continent.

I tried to get the result like,

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))

Is there an elegant way to do this?

+4
source share
2 answers

You can have the nested list you are looking for with this code:

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

You can use the data.treepackage:

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  

Or another option:

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  
+1
source

Source: https://habr.com/ru/post/1663007/


All Articles