0

I wrote a function that reads in data and applys some transformation. Now I would like to write a for loop to get the different files I want and assign them as dataframes to a list. Unfortunately, I do not understand the syntax of the procedure I guess.

> list_of_df <- {} 
> for (i in 10:20) { 
> df <- read_my_data(i)
> list_of_df[[i]] <- df 
> }

my function to read in the data works perfectly fine and returns a dataframe. i is used as the argument in the function to identify the correct data. I struggle to attach the dataframes that result from my function to the empty list at the correct place.

Would be great if you could tell me how that mechanism is supposed to work...

the result should be like:

list_of_df 
$10
....
$11
....
....
$20

I know that it works for vectors like:

> empty_vector <- vector()  
> for (i in 1:10) {
> empty_vector[i] <- i
>  }

but if I do it from higher values than 1, I get NAS and I am also unable to transfer it to dataframes and lists...

Thank you for your help

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • The equivalent of `empty_vector <- vector()` is `empty_list <- list()`. If you change `list_of_df <- {}` to `list_of_df <- list()`, it should work as well as your function does. – Gregor Thomas Jun 26 '23 at 15:16
  • For more details, see my answer at the FAQ [How to make a list of data frames?](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) – Gregor Thomas Jun 26 '23 at 15:18
  • Though there is some weirdness that you aren't starting at 1. R might be a bit confused if you create an empty list and then the first item you assign is the 10th one, not the 1st one. You could either (a) convert the integers to character while assigning, `list_of_df[[as.character(i)]] <- read_my_data(i)` so you are assigning by name rather than by number, or (b) use `lapply` as a shortcut to replace the `for` loop and the empty list declaration entirely with `list_of_df <- lapply(10:20, read_my_data)` and then name the list items `names(list_of_df) <- 10:20`. – Gregor Thomas Jun 26 '23 at 15:23
  • Thank you so much! I will try the lapply approach first, Will take a while, is a huge dataset. – user21903146 Jun 26 '23 at 15:23
  • Start on a sample to see if it works! `list_of_df = lapply(10:11, read_my_data)`. You'll be much faster at solving problems if you test and debug on a small example before you try it on a huge one. – Gregor Thomas Jun 26 '23 at 15:24
  • 1
    it works perfectly! Thank you! – user21903146 Jun 26 '23 at 17:21

0 Answers0