-3

I am trying to open many .csv files in a folder, and read one value in each csv file. The csv files have the same structure, but different names, such as factory.01.csv, factory.02.csv, etc. Is it possible to read them in R using a loop? If so, how to write the loop? Thanks.

mountain_sn
  • 53
  • 1
  • 2
  • 10

1 Answers1

1
    setwd("WHATEVER-YOUR-WD-PATH-IS")

#Create a list of all file names in directory
#Save File names to a variable

    filenames <- list.files(pattern="factory+.*csv")

## Get names without ".CSV" and store in "names"

    names <- substr(filenames, 1, 10)

## Read in all data frames using a loop

    for(i in names){
      filepath <- file.path(paste(i,".csv",sep=""))
      assign(i, read.csv(filepath, sep = ",", header=FALSE, skip=1))
    }
vagabond
  • 3,526
  • 5
  • 43
  • 76