0

I need to mass-import some data for my R project. Following some guide, I wrote a simple for loop which goes like that:

for (for_variable in list.files(path = "./data", pattern = ".csv$")) {
  temp <- read_csv(for_variable)
  # Some data wranglig
  database <- rbind(database, temp)
  rm(temp)
  }

The problem is that my data is in the data folder in my working directory, as I've specified in list.files(path = "./data"). The problem is that I can't use read_csv(for_variable) because I get an error:

'file_name.csv' does not exist in current working directory

And if I try to specify the path in read_csv, it doesn't understand what 'for_variable' is, it tries to find literal 'for_variable' file in the data folder. So how can I combine path and variable name in read_csv? Or is there any other way of solving the problem?

  • 1
    (1) Use `list.files(..., full.names=TRUE)`, as EJJ suggested. (2) Do not iteratively build data, it is horribly inefficient, create a `list` of frames and then do one call of `database – r2evans Apr 24 '21 at 16:06
  • @r2evans I'd love not to use the loop but I don't know how to do the wrangling if I don't. I want to slice, select and mutate my temporary data before binding it because the files I got are quite messy. Luckily they're all messy in the same way so I can clean them all with: `temp %<>% slice(4:11) %>% select(form_2.index, form_2.response) %>% mutate(plec = as.character(temp[1,4]), wiek = as.double(temp[2,4]), uczestnik = as.character(temp[1,13]), warunek = as.character(temp[1,16]), data = ymd_hm(temp[1,15]))` How do I do it without the loop? – Jakub Jędrusiak Apr 24 '21 at 17:22
  • I've made a question out of it https://stackoverflow.com/questions/67252097/how-to-address-specific-cell-in-lapply – Jakub Jędrusiak Apr 25 '21 at 10:03

1 Answers1

3

I would recommend reading this post as it is helpful for importing multiple csv files.

But to help with your specific question, your error is likely caused becauseo you need to pass the full path name for the files you want to import and that can be specified by using the full.names = TRUE argument in list.files(). Passing just the file name contained in for_variable to read_csv won't work.

list.files(path = "./data", full.names = TRUE, pattern = ".csv$")
EJJ
  • 1,274
  • 7
  • 16