I'm splitting a data frame into three separate data frames and then applying a function to each of those separate data frames. How can I do this with a for loop (saving more than just the last dataset)? And how can I do this with lapply? (When I do it here, I get the error: Error in UseMethod("filter") : no applicable method for 'filter' applied to an object of class "c('double', 'numeric')"
(Mt cars mpg class is numeric.)
When I'm doing a similar thing in my use-case (many different tidy verse functions, etc.), I'm getting the error Error in `select()`: ! `select()` doesn't handle lists.
(I do see this answer: R: `select()` doesn't handle lists it seems like a pretty common thing to implement larger functions with select statements in lapply.
MRE below.
dfs_cars = split(mtcars, ~cyl)
filter_mpg <- function(dat, mpg_greater){
dat%>%
filter(mpg > mpg_greater)
}
for (i in dfs_cars){
ns = i%>%filter_mpg(21)
}
lapply(dfs_cars, filter_mpg(21))
To use a for loop, you should prepare an object to hold the result of each iteration of the loop. I made the OutList list for this. I then iterate over an index and increment the position in both dfs_cars and OutList.
To use lapply, the second argument should be the bare function name, not filter_mpg(21), and the second argument of the function can be supplied separately in the ... argument of lapply.
library(tidyverse)
dfs_cars = split(mtcars, ~cyl)
OutList <- vector(mode = "list", length = length(dfs_cars))
filter_mpg <- function(dat, mpg_greater){
dat%>%
filter(mpg > mpg_greater)
}
for (i in seq_along(dfs_cars)){
OutList[[i]] = dfs_cars[[i]] %>% filter_mpg(21)
}
OutList
#> [[1]]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
#> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
#> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
#> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
#> Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
#>
#> [[2]]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#>
#> [[3]]
#> [1] mpg cyl disp hp drat wt qsec vs am gear carb
#> <0 rows> (or 0-length row.names)
OutList2 <- lapply(dfs_cars, filter_mpg, mpg_greater = 21)
OutList2
#> $`4`
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
#> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
#> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
#> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
#> Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
#>
#> $`6`
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#>
#> $`8`
#> [1] mpg cyl disp hp drat wt qsec vs am gear carb
#> <0 rows> (or 0-length row.names)
Created on 2024-10-21 with reprex v2.1.1