HonestRlover.not.
HonestRlover.not.

Reputation: 67

read multiple csv files with timestamp in R

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

Upvotes: 1

Views: 358

Answers (1)

Joshua Mire
Joshua Mire

Reputation: 736

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!

Upvotes: 3

Related Questions