1

I found several questions about how to read multiple csv files into R. However, they are all named sequentially. Now, I've got files with time stamps. So I find it difficult to find a way to read them all in. Can somebody help? For example, they look like this:

99_Experiment Version A_2020-06-02_12h26.48.883.csv

I don't need the date, it was just automatically generated. Is there an easy way to remove it? Then all files would be named in the same format (_Experiment Version A.csv with ascending numbers at the front) and I can maybe use one of the other posts to read them

1 Answers1

3

Below are some options you can try depending on how you want to import your data into R.

Note: make sure to set your working director to the folder your files are contained in with the setwd() function.

Below will result in each data frame as a separate element in a single list:

temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)

If you then want to combine those list elements into a single data frame, you can use functions like do.call(rbind,...), dplyr::bind_rows(), or data.table::rbindlist().

If you really want your .csv file as a separate data frames you could do the following with assign():

temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))

Or you could use the below code which does not rely upon the assign() function:

temp = list.files(pattern="*.csv")
list2env(
  lapply(setNames(temp, make.names(gsub("*.csv$", "", temp))), 
         read.csv), envir = .GlobalEnv)

I hope this helps!

Joshua Mire
  • 736
  • 1
  • 6
  • 17
  • Hey, I saw this solution earlier, but if I use the functions you suggested I only get a data frame with one column (the entries of the earlier columns are separated by , each) – HonestRlover.not. Jun 03 '20 at 12:59
  • 1
    How many columns is it supposed to have? Is there a first column that is being read as a row label? If so you can set `row.names=FALSE` within the `read.csv()` function. Also, you can set `sep=","` which should tell the function your data is seperated by commas. Not sure if this will work though, `sep=","` is supposed to be the default. – Joshua Mire Jun 03 '20 at 13:04
  • It's supposed to have 63 columns, and no, the first one is already filled with data, I have a header. – HonestRlover.not. Jun 03 '20 at 13:09
  • Does `temp = list.files(pattern="*.csv")` at least generate a complete list of your `.csv` files? – Joshua Mire Jun 03 '20 at 13:11
  • Did you try adjusting `read.csv(temp[i])` to read `read.csv(temp[i], head = TRUE, sep = ",")` in the second chunk of code I posted? The result would be the line: `for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i], head = TRUE, sep = ","))`. – Joshua Mire Jun 03 '20 at 13:27
  • If this ended up working, please select this answer to close out the question. Best wishes! – Joshua Mire Jun 03 '20 at 16:10