0

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!

Tino
  • 2,091
  • 13
  • 15
CloverCeline
  • 511
  • 3
  • 18

2 Answers2

0

You may use this general syntax for lapply:

lapply(df, function(x) read.csv(x))

In other words, you may specify an anonymous function to be applied to df. Applying this to your actual code:

temp <- list.files(pattern=".csv")
list2env(lapply(setNames(temp, make.names(gsub(".csv$", "", temp))),
    function(x) read.csv(x, row.names=1)), envir = .GlobalEnv)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

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
)
Tino
  • 2,091
  • 13
  • 15