I have a list of dataframes and I want to apply a custom function to it using lapply
Here is my function:
rename_cols_pattern <- function (df, pattern, replacement = "") {
names(df) <- gsub(names(df), pattern = pattern, replacement = replacement)
}
How do I use this function with lapply? This does not work because the df variable is missing. How do I pass in the df variable which would be the dataframes in the di_data list
di_data <- lapply(di_data, rename_cols_pattern(pattern = "X"))
I can get this to work like so:
di_data <- lapply(di_data, function(x) {
names(x) <- gsub(names(x), pattern = "X", replacement = "")
x
})
However I want the function to be separate and want to understand how to achieve this