0

Novice R user here...

I'm using the code below to import all csv files in a folder:

path <- "C:/Users/Daniel/Desktop/Motors/"
files <- list.files(path=path, pattern="*.csv")
for(file in files)
{
  perpos <- which(strsplit(file, "")[[1]]==".") 
  assign(
    gsub(" ","",substr(file, 1, perpos-1)), 
    read.csv(paste(path,file,sep="")))
}

These CSV files actually act as Lookup tables later in the code. To avoid user error, I'm curious as to whether it's possible, to convert any character data (not headings) within each dataframe to upper case as the files are imported.

Of course, I could just change each of the csv files, manually, but would rather avoid doing this.

dking16
  • 1
  • 2

1 Answers1

1

Here is a solution: lapply toupper to each character variable within your data.frame at time of import.

assign(
  gsub(" ","",substr(file, 1, perpos-1)), 
  lapply(read.csv(paste(path,file,sep="")),
    function(x) {
      if(class(x) == "character") toupper(x)
      else if(is.factor(x)) factor(toupper(x))
      else x
    }
   ))

However, I strongly recommend you to not use assign to programmatically create data.frames. You should rather import your data.frames inside a list.

scoa
  • 19,359
  • 5
  • 65
  • 80