# across() -----------------------------------------------------------------
# Different ways to select the same set of columns
# See for details
iris %>%
as_tibble() %>%
mutate(across(c(Sepal.Length, Sepal.Width), round))
iris %>%
as_tibble() %>%
mutate(across(c(1, 2), round))
iris %>%
as_tibble() %>%
mutate(across(1:Sepal.Width, round))
iris %>%
as_tibble() %>%
mutate(across(where(is.double) & !c(Petal.Length, Petal.Width), round))
# A purrr-style formula
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), ~ mean(.x, na.rm = TRUE)))
# A named list of functions
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd)))
# Use the .names argument to control the output names
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), mean, .names = "mean_{.col}"))
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd), .names = "{.col}.{.fn}"))
# When the list is not named, .fn is replaced by the function's position
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), list(mean, sd), .names = "{.col}.fn{.fn}"))
# across() returns a data frame, which can be used as input of another function
df <- data.frame(
x1 = c(1, 2, NA),
x2 = c(4, NA, 6),
y = c("a", "b", "c")
)
df %>%
mutate(x_complete = complete.cases(across(starts_with("x"))))
df %>%
filter(complete.cases(across(starts_with("x"))))
# if_any() and if_all() ----------------------------------------------------
iris %>%
filter(if_any(ends_with("Width"), ~ . > 4))
iris %>%
filter(if_all(ends_with("Width"), ~ . > 2))
Run the code above in your browser using DataLab