# compare_conditions works similar to dplyr::across()
pixar_films |>
compare_conditions(
x = (rating == "G"),
y = (rating == "PG"),
.cols = rotten_tomatoes
)
# because data frames are just fancy lists, you pass the result to headline_list()
pixar_films |>
compare_conditions(
x = (rating == "G"),
y = (rating == "PG"),
.cols = rotten_tomatoes
) |>
headline_list("a difference of {delta} points")
# you can return multiple objects to compare
# 'view_List()' is a helper to see list objects in a compact way
pixar_films |>
compare_conditions(
x = (rating == "G"),
y = (rating == "PG"),
.cols = c(rotten_tomatoes, metacritic),
.fns = dplyr::lst(mean, sd)
) |>
view_list()
# you can use any of the `tidyselect` helpers
pixar_films |>
compare_conditions(
x = (rating == "G"),
y = (rating == "PG"),
.cols = dplyr::starts_with("bo_")
)
# if you want to compare x to the overall average, use y = TRUE
pixar_films |>
compare_conditions(
x = (rating == "G"),
y = TRUE,
.cols = rotten_tomatoes
)
# to get the # of observations use length() instead of n()
# note: don't pass the parentheses
pixar_films |>
compare_conditions(
x = (rating == "G"),
y = (rating == "PG"),
.cols = rotten_tomatoes, # can put anything here really
.fns = list(n = length)
)
# you can also use purrr-style lambdas
pixar_films |>
compare_conditions(
x = (rating == "G"),
y = (rating == "PG"),
.cols = rotten_tomatoes,
.fns = list(avg = ~ sum(.x) / length(.x))
)
# you can compare categorical data with functions like dplyr::n_distinct()
pixar_films |>
compare_conditions(
x = (rating == "G"),
y = (rating == "PG"),
.cols = film,
.fns = list(distinct = dplyr::n_distinct)
)
Run the code above in your browser using DataLab