Reputation: 680
I've made an outlier function and would like to apply it to a list of dataframes. So far unsuccessfully. Is this a job for lapply, or sapply?
# Remove outliers
outlier <- function(x) {
x[x < quantile(x,0.25) - 6 * IQR(x) | x > quantile(x,0.75) + 6 * IQR(x)] <- NA
x
}
Upvotes: 1
Views: 245
Reputation: 887991
We can use lapply
to loop over the list
of data.frame
, then the function is applied on each column by looping over the columns (lapply
) and assign the output back
lapply(lst1, function(x) {x[] <- lapply(x, outlier)
x })
If the columns are of mixed types, we select the numeric columns and apply the function
lapply(lst1, function(x) {i1 <- sapply(x, is.numeric)
x[i1] <- lapply(x[i1], outlier)
x
})
Or using tidyverse
library(tidyverse)
map(lst1 ~ .x %>%
mutate_if(is.numeric, outlier))
Upvotes: 2