Reputation: 45
I would like to inflate data for 2020 by a certain percentage based on what category it is. The data goes from 2017-2021 (52 weeks in each) and I have 4 different categories I need to filter by. I have attached a sample of the data
Week | Category | Qty |
---|---|---|
202001 | Category1 | 100 |
202001 | Category2 | 115 |
202002 | Category1 | 95 |
202002 | Category2 | 105 |
Upvotes: 1
Views: 59
Reputation: 16836
If you only have 4 categories, then you can make a series of ifelse
statements using case_when
.
library(tidyverse)
df %>%
mutate(inflate = case_when(Category == "Category1" ~ (Qty*0.05)+Qty,
Category == "Category2" ~ (Qty*0.25)+Qty,
Category == "Category3" ~ (Qty*0.30)+Qty,
Category == "Category4" ~ (Qty*0.45)+Qty,
TRUE ~ NA_real_))
Output
Week Category Qty inflate
1 202001 Category1 100 105.00
2 202001 Category2 115 143.75
3 202002 Category1 95 99.75
4 202002 Category2 105 131.25
5 202002 Category3 105 136.50
6 202002 Category4 105 152.25
Or if you did have a lot of categories, then you could create a dataframe key for the percentages for each category. Then, you could join them together and make the calculation for each row.
df_perc <- data.frame(Category = c("Category1", "Category2", "Category3", "Category4"),
Percent = c(0.05, 0.25, 0.30, 0.45))
df %>%
left_join(., df_perc, by = "Category") %>%
mutate(inflate = (Qty*Percent)+Qty)
Data
df <,- structure(list(Week = c(202001L, 202001L, 202002L, 202002L, 202002L,
202002L), Category = c("Category1", "Category2", "Category1",
"Category2", "Category3", "Category4"), Qty = c(100L, 115L, 95L,
105L, 105L, 105L)), class = "data.frame", row.names = c(NA, -6L
))
Upvotes: 1