Hello all,
I have a few functions that I often run over multiple files using list.files()
and lapply()
. If we assume each file contains 2 columns of data (V1 and V2), a very basic example might look something like this:
#(step 1) list of file paths
mylist <- list.files(path = "C:/Data",
pattern = "*.csv",
full.names = T)
#(step 2) create function to, for example, get r squared from lm().
myfunction <- function(x) {
data = read.csv(x)
id = basename(x)
r2 = summary(lm(V1 ~ V2, data))$r.squared
df = cbind(id, r2)
return(df)
}
#(step 3) apply function to list and bind rows to
#get 1 data frame with results from all files
final <- lapply(mylist,
FUN = myfunction) %>%
bind_rows(.id = "column_label")
My question would then be, is there a way to consolidate steps 1,2, and 3 above so that it would effectively be one function? Ie. the list.files()
and lapply()
steps would sit with in the function. It would be nice to be able to call the function simply as myfunction(path = "C:/Data")
which would then spit out the final
dataframe. Could I wrap another function around it, is that a legit way of doing this?
Any thoughts and ideas would be very much appreciated.
Manny thanks,
Matt