Read all files in a folder and apply a function to each data frame

On the contrary, I do think working with list makes it easy to automate such things. Here is one solution (I stored your four dataframes in folder temp/). filenames <- list.files(“temp”, pattern=”*.csv”, full.names=TRUE) ldf <- lapply(filenames, read.csv) res <- lapply(ldf, summary) names(res) <- substr(filenames, 6, 30) It is important to store the full path for …

Read more

Access lapply index names inside FUN

Unfortunately, lapply only gives you the elements of the vector you pass it. The usual work-around is to pass it the names or indices of the vector instead of the vector itself. But note that you can always pass in extra arguments to the function, so the following works: x <- list(a=11,b=12,c=13) # Changed to …

Read more

Why use purrr::map instead of lapply?

If the only function you’re using from purrr is map(), then no, the advantages are not substantial. As Rich Pauloo points out, the main advantage of map() is the helpers which allow you to write compact code for common special cases: ~ . + 1 is equivalent to function(x) x + 1 (and \(x) x …

Read more