Search code examples
rread.csv

Importing multiple .csv files into R adding condition (row.names=1)


Have find original question and solution here: Importing multiple .csv files into R

solution is:

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

but I want to add one condition to read.csv, row.names=1 --the first column is row name then the solution does not work anymore. Any suggestion?

was using:

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

Thank you!


Solution

  • If you call a function using lapply, you don't write arguments in braces as you would when you call the function itself. Instead, just add the argument like:

    list2env(
      x = lapply(
        X = setNames(temp, make.names(gsub("*.csv$", "", temp))), 
        FUN = read.csv, row.names = 1
      ), 
      envir = .GlobalEnv
    )