I need help to make a list of data frames in R

Viewed 70

I read my data frame and stored it in a variable. Then I created an empty list. I created a loop which goes through the columns and makes a dataframe from the select() function and assigns it to the next position in the list. However, that is not what is happening, only the columns are being stored in the list.

here is my code:

library(tidyverse)

data = read_csv('hubbard_data.csv')
columns = colnames(data)
#make date and time correct
data$TIMESTAMP_START = strptime(data$TIMESTAMP_START,"%Y%m%d%H%M")
data$TIMESTAMP_END = strptime(data$TIMESTAMP_END,"%Y%m%d%H%M")
dfs = list() #List of data frames
it = 1 #Iterator
while(it <= length(columns)){
      dfs[it] <- data %>% select(columns[it],LE)
      it = it + 1
 }

I did a little further analysis and found this:

temp1 <- data %>% select(columns[1],LE)

dfs[1] <- data %>% select(columns[1],LE)

temp1 is a data.frame but dfs[1] is not. Why?

3 Answers

You can split a data frame into a list of single-column data frames by doing:

my_list <- lapply(as.list(data), as.data.frame)

If you want to add the column LE from your original data frame to all the data frames in your list then you can do

my_list <- lapply(my_list, function(x) { x$LE <- data$LE; return(x) })

Here's one way of doing this:

library(data.table)
dat <- as.data.table(mtcars)
mylist <- lapply(colnames(dat), function(z){
  return_table <- dat[, .(get(z))]
  names(return_table) <- z
  return(return_table)
  })

Checking the output:

> str(mylist[[1]])
Classes ‘data.table’ and 'data.frame':  32 obs. of  1 variable:
 $ mpg: num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 - attr(*, ".internal.selfref")=<externalptr> 
> str(mylist[[5]])
Classes ‘data.table’ and 'data.frame':  32 obs. of  1 variable:
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 - attr(*, ".internal.selfref")=<externalptr> 

If you want more than one column (say mpg must always be there), you can change the below two line as shown below:

  return_table <- dat[, .(mpg, get(z))]
  names(return_table) <- c('mpg', z)

Output with this change:

> str(mylist[[5]])
Classes ‘data.table’ and 'data.frame':  32 obs. of  2 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 - attr(*, ".internal.selfref")=<externalptr> 

try

dfs[[it]] <- data %>% select(columns[it],LE)

instead of

dfs[it] <- data %>% select(columns[it],LE)
Related