0

I would like to read data from several files into separated data frames. The files are in a different folder than the script. I have used list with filenames.

users_list <- list.files(path = "Data_Eye/Jazz/data/first",
                         pattern = "*.cal", full.names = F)

I tried to use functions map and read_delim but without success. It is important for me to read each file to a different dataframe. It will be the best to have the list of data frames.

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Karls
  • 31
  • 1
  • [Don't ever create d1 d2 d3, ..., dn in the first place. Create a list d with n elements.](https://stackoverflow.com/a/24376207/1422451) – Parfait Jul 05 '21 at 23:36

2 Answers2

0

You can do something like this, though I don't have any .cal files to test it on. So, it's possible you might need a different function for reading in those files.

library(devtools)
devtools::install_github("https://github.com/KirtOnthank/OTools")
library(OTools)

# Give full path to your files (if different than working directory).
temp = list.files(path="../Data/original_data", pattern="*.cal", full.names = TRUE)

# Then, apply the read.cal function to the list of files.
myfiles = lapply(temp, OTools::read.cal)

# Then, set the name of each list element (each dataframe) to its respective file name.
names(myfiles) <- gsub(".cal","",
                       list.files("../Data/original_data",full.names = FALSE),
                       fixed = TRUE)

# Now, put all of those individual dataframes from your list into the global environment as separate dataframes.
list2env(myfiles,envir=.GlobalEnv)
AndrewGB
  • 16,126
  • 5
  • 18
  • 49
0

In base R, just use lapply to generate a list of data frames:

list_of_dfs <- lapply(users_list, read_delim)
ngwalton
  • 383
  • 3
  • 8