SDS 192: Introduction to Data Science
Lindsay Poirier
Statistical & Data Sciences, Smith College
Fall 2022
across()
variablesacross()
purrr
packagetidyverse
map()
functionsmap()
functions allow us to apply a function to each element of a list or vectorpull()
map()
set_names()
sets the names of elements in a vectorcreate_total_col <- function(x){
df |>
filter(name == x) |>
mutate(total = var_a + var_b + var_c)
}
map(unique(df$name), create_total_col)
[[1]]
name var_a var_b var_c total
1 obs1 2 4 4 10
2 obs1 5 1 2 8
[[2]]
name var_a var_b var_c total
1 obs2 3 7 9 19
[[3]]
name var_a var_b var_c total
1 obs3 4 2 3 9
[[1]]
[1] 6
[[2]]
[1] 10
[[3]]
[1] 6
[[4]]
[1] 6