Home > Blockchain >  Extracting multiple dataframes from a list of dataframes in R
Extracting multiple dataframes from a list of dataframes in R

Time:10-08

I have a list of data frames, structured like this.

list of dataframes

I need to extract a variable number of elements from said list, and will just be joining them together using an identifier after I'm finished.

Extraction using the [[]] method works, but I cannot seem to get it to take a range.

This works:

x <- pf_metrics[[[1]]]

It extracts the entire first dataframe, in the original structure. This is what I would like to do, but across a variable length of dataframe lists.

Using syntax like...

x <- pf_metrics[c[1:length(pf_metrics)]]

OR

x <- pf_metrics[[c(1,2)]]

Doesn't seem to work either.

Thanks!

CodePudding user response:

Try the following -

num <- c(1, 2)
result <- dplyr::bind_rows(pf_metrics[num], .id = 'id')

where num are the list elements that you want to select and it will also include an id column which would be unique identifier of each list.

CodePudding user response:

Using data.table

library(data.table)
num <- c(1, 2)
result <- rbindlist(pf_metrics[num], idcol = 'id')
  • Related