Home > front end >  How to summarize a portion of a group of lists into a table
How to summarize a portion of a group of lists into a table

Time:01-25

I am trying to pull the results of several queries into a single table. All of the results are in a list of lists, often as single numbers from a statistical evaluation. I also need to be able to specify which of the lists results I want to tabulate. For an example set of lists:

list1 <- list(1, 2, 3, 4)
  names(list1) <- c("first", "second", "third", "fourth")
list2 <- list(10, 11, 12, 13)
  names(list2) <- c("first", "second", "third", "fourth")
list3 <- list(100, 101, 102, 103)
  names(list3) <- c("first", "second", "third", "fourth")

I can grab the results individually from each list:

library(magrittr)

row1 <- list1[c("second", "third")] %>%
  rbind() %>%
  unlist()

row2 <- list2[c("second", "third")] %>%
  rbind() %>%
  unlist()

row3 <- list3[c("second", "third")] %>%
  rbind() %>%
  unlist()

then add them to a data frame, then name each column, and final print the result

library(kableExtra)
desired_table <- rbind(row1, row2, row3)
colnames(desired_table) <- c("second", "third")

kbl(desired_table)

It seems to me like there should be an easier way, and I expect there is.

How can I do this with a list of package results, either with another package or in a way that I have not thought of?

CodePudding user response:

Here's a base application of a composition of a couple of standard R functions that does it as a one liner:

t( sapply( list(list1,list2,list3), "[", c("second", "third") ) )
     second third
[1,] 2      3    
[2,] 11     12   
[3,] 101    102  
library(kableExtra)
desired_table <- t( sapply(list(list1,list2,list3), "[", c("second", "third") ) )
png()
kbl(desired_table); dev.off()

Might want to add a bit of space between those two columns:

enter image description here

CodePudding user response:

We could use modify_depth from purrr package:

library(purrr)

my_nested_list <- list(list1, list2, list3)

second <- modify_depth(my_nested_list, 1, "second") %>% 
  unlist()
third <- modify_depth(my_nested_list, 1, "third") %>% 
  unlist()

desired_table <- cbind(second, third)
colnames(desired_table) <- c("second", "third")

desired_table
     second third
[1,]      2     3
[2,]     11    12
[3,]    101   102
  • Related