0

I'm trying to import multiple .csvs each with different dimensions. I want to create a tibble for each .csv and keep the same file name for the tibble, so that I have the same number of dfs in global environment as I have .csvs.

Working directory is "Project" and .csvs are in "Project/Data"

temp <- list.files(path = "Data")
all <- lapply(temp, read.csv)

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'circuits.csv': No such file or directory

I'm also not sure if read.csv needs arguments within lapply().

edit: I've managed to get the 2nd part working but I get a large list with dataframes instead of the data frames that I originally wanted.

All <- lapply(temp, function(i){
  read.csv(i, header = TRUE, sep = ",")
})

Large list with dataframes

Phil
  • 7,287
  • 3
  • 36
  • 66
krexaim
  • 13
  • 4
  • 2
    add the argument `full.names=TRUE` to the `list.files` function – rocastocks Feb 21 '22 at 19:50
  • adding the argument didn't do anything except add a "Data/" prefix to my previous list – krexaim Feb 21 '22 at 20:04
  • well, adding the argument fixed the first error. to add each data.frame to the global environment you can use the `assign` function e.g. `x<- read.csv(i) assign(i, x)` – rocastocks Feb 21 '22 at 20:19
  • 1
    Don't use `assign`, it's a bad habit to get into (IMO). It's generally best to keep things as a [list of frames](https://stackoverflow.com/a/24376207/3358227), read the link for many discussions about it. One thing you can do to help is to name the list with the filenames, perhaps either `names(alldat) <- temp` or `names(alldat) <- basename(temp)` if all filenames are unique and you don't want/need the directory in the object name. From there, you would do `alldat[["Data/circuits.csv"]]` (or without Data/). BTW, I recommend against using `all` as a variable name, it's a common R function. – r2evans Feb 21 '22 at 20:28

0 Answers0