Skip to content

Instantly share code, notes, and snippets.

@AlbertRapp
Created April 21, 2023 08:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlbertRapp/deb3fd0ee2478687c787b3b45a230aab to your computer and use it in GitHub Desktop.
Save AlbertRapp/deb3fd0ee2478687c787b3b45a230aab to your computer and use it in GitHub Desktop.
maps_instead_of_for_loops.R
library(tidyverse)
dat <- gapminder::gapminder
####### For loop approach
slopes <- numeric(5)
names(slopes) <- unique(dat$continent)
for (selected_continent in unique(dat$continent)) {
filtered_data <- dat |>
filter(continent == selected_continent)
lin_mod <- lm(
data = filtered_data,
lifeExp ~ year
)
slopes[selected_continent] <- coefficients(lin_mod)[2]
}
slopes
##### Using maps instead
nested_data <- dat |>
group_by(continent) |>
nest() |>
ungroup()
nested_data |>
mutate(
lin_mod = map(
data,
function(x) lm(data = x, lifeExp ~ year)
),
coefficients = map(lin_mod, coefficients),
slope = map_dbl(coefficients, \(x) x[2]),
slope_short = map_dbl(coefficients, 2)
)
map_dbl(1:5, \(x) x + 5)
extract_continent <- function(x) filter(dat, continent == x)
run_lifeExp_model <- function(x) lm(data = x, lifeExp ~ year)
extract_slopes <- function(x) coefficients(x)[2]
unique(dat$continent) |>
map(extract_continent) |>
map(run_lifeExp_model) |>
map_dbl(extract_slopes)
tibble(
x = 1:5,
y = 2:6,
sum = map2_dbl(x, y, \(x, y) x + y)
)
tibble(
x = c(1, 1, 1),
y = c(10, 20, 30),
z = c(100, 200, 300),
sum = pmap_dbl(list(x, y, z), sum)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment