Last active
May 20, 2025 19:11
-
-
Save seankross/813e18789ef8dd5cefe1829c72edb52f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
fruit_prices <- tibble( | |
Month = c("Jan", "Feb", "Mar"), | |
Apples = c(1.79, 1.82, 1.8), | |
Pears = c(1.29, 1.34, 1.5), | |
Oranges = c(.89, .92, .85) | |
) | |
# A typical mutate, create a new column that is the sum of three columns | |
fruit_prices %>% | |
mutate(Sum = Apples + Pears + Oranges) | |
# # A tibble: 3 × 5 | |
# Month Apples Pears Oranges Sum | |
# <chr> <dbl> <dbl> <dbl> <dbl> | |
# 1 Jan 1.79 1.29 0.89 3.97 | |
# 2 Feb 1.82 1.34 0.92 4.08 | |
# 3 Mar 1.8 1.5 0.85 4.15 | |
# Mutate with rowwise + c_across to select individual columns by name | |
fruit_prices %>% | |
rowwise() %>% | |
mutate(Sum = sum(c_across(c("Apples", "Pears", "Oranges")))) %>% | |
ungroup() | |
# # A tibble: 3 × 5 | |
# Month Apples Pears Oranges Sum | |
# <chr> <dbl> <dbl> <dbl> <dbl> | |
# 1 Jan 1.79 1.29 0.89 3.97 | |
# 2 Feb 1.82 1.34 0.92 4.08 | |
# 3 Mar 1.8 1.5 0.85 4.15 | |
# Same as above but uses the `:` shorthand, selects all columns between Apples and Oranges | |
fruit_prices %>% | |
rowwise() %>% | |
mutate(Sum = sum(c_across(Apples:Oranges))) %>% | |
ungroup() | |
# # A tibble: 3 × 5 | |
# Month Apples Pears Oranges Sum | |
# <chr> <dbl> <dbl> <dbl> <dbl> | |
# 1 Jan 1.79 1.29 0.89 3.97 | |
# 2 Feb 1.82 1.34 0.92 4.08 | |
# 3 Mar 1.8 1.5 0.85 4.15 | |
# Sum only the Apples and Oranges columns | |
fruit_prices %>% | |
rowwise() %>% | |
mutate(`A+O Sum` = sum(c_across(c("Apples", "Oranges")))) %>% | |
ungroup() | |
# # A tibble: 3 × 5 | |
# Month Apples Pears Oranges `A+O Sum` | |
# <chr> <dbl> <dbl> <dbl> <dbl> | |
# 1 Jan 1.79 1.29 0.89 2.68 | |
# 2 Feb 1.82 1.34 0.92 2.74 | |
# 3 Mar 1.8 1.5 0.85 2.65 | |
# Using contains from tidyselect, only ums columns that contain "p" | |
fruit_prices %>% | |
rowwise() %>% | |
mutate(P_Sum = sum(c_across(contains("p")))) %>% | |
ungroup() | |
# # A tibble: 3 × 5 | |
# Month Apples Pears Oranges P_Sum | |
# <chr> <dbl> <dbl> <dbl> <dbl> | |
# 1 Jan 1.79 1.29 0.89 3.08 | |
# 2 Feb 1.82 1.34 0.92 3.16 | |
# 3 Mar 1.8 1.5 0.85 3.3 | |
# Drop Month and sum all the remaining columns | |
fruit_prices %>% | |
select(-Month) %>% | |
rowwise() %>% | |
mutate(Sum = sum(c_across(everything()))) %>% | |
ungroup() | |
# # A tibble: 3 × 4 | |
# Apples Pears Oranges Sum | |
# <dbl> <dbl> <dbl> <dbl> | |
# 1 1.79 1.29 0.89 3.97 | |
# 2 1.82 1.34 0.92 4.08 | |
# 3 1.8 1.5 0.85 4.15 | |
# Only sum the numeric columns | |
fruit_prices %>% | |
rowwise() %>% | |
mutate(Sum = sum(c_across(where(is.numeric)))) %>% | |
ungroup() | |
# # A tibble: 3 × 5 | |
# Month Apples Pears Oranges Sum | |
# <chr> <dbl> <dbl> <dbl> <dbl> | |
# 1 Jan 1.79 1.29 0.89 3.97 | |
# 2 Feb 1.82 1.34 0.92 4.08 | |
# 3 Mar 1.8 1.5 0.85 4.15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment