I'm trying to get a summary going with a condition. I can't seem to get the syntax correct to do what I'm trying.
I want to add a column called percent to compare how much of the total is part of a given segment (industry).
library(tidyverse)
df <- tibble(
year = c(2020,2020,2020,2020,2020),
zip_code = c(90001,90001,90001,90001,90001),
industry = c("Tech","Wholesale","Tech", "Manufacturing", "Wholesale"),
total = c(30500353,12958359,2638403,559844,9177618)
)
df %>% group_by(industry) %>% summarize(total = sum(total),
percent = total/ sum(if_else(industry == "Tech")))
#> Error: Problem with `summarise()` column `percent`.
#> i `percent = total/sum(if_else(industry == "Tech"))`.
#> x argument "true" is missing, with no default
#> i The error occurred in group 1: industry = "Manufacturing".