lapply
Access and preserve list names in lapply function
I believe that lapply by default keeps the names attribute of whatever you are iterating over. When you store the names of myList in n, that vector no longer has any “names”. So if you add that back in via, names(n) <- names(myList) and the use lapply as before, you should get the desired result. …
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 …
passing several arguments to FUN of lapply (and others *apply)
If you look up the help page, one of the arguments to lapply is the mysterious …. When we look at the Arguments section of the help page, we find the following line: …: optional arguments to ‘FUN’. So all you have to do is include your other argument in the lapply call as an …
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 …
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 …
Grouping functions (tapply, by, aggregate) and the *apply family
R has many *apply functions which are ably described in the help files (e.g. ?apply). There are enough of them, though, that beginning useRs may have difficulty deciding which one is appropriate for their situation or even remembering them all. They may have a general sense that “I should be using an *apply function here”, …