below is my data
data <-tribble(
~fruit, ~transaction, ~season, ~n,
"a", "sell", "s", 14,
"a", "sell", "f", 23,
"a", "buy", "s", 43,
"a", "buy", "f", 15,
"a", "keep", "s", 24,
"a", "keep", "f", 5,
"o", "sell", "s", 10,
"o", "sell", "f", 11,
"o", "buy", "s", 6,
"o", "buy", "f", 9,
"o", "keep", "s", 40,
"o", "keep", "f", 10,
"b", "sell", "s", 7,
"b", "sell", "f", 0,
"b", "buy", "s", 10,
"b", "buy", "f", 12,
"b", "keep", "s", 30,
"b", "keep", "f", 2
) %>% as.data.frame()
I want to see what percentage of sells in season s were from fruit a, which is 14/(14+10+7)
or what percentage of keepss in season f were from fruit b, 2/(2+10+5)
and replace these frequency numbers with the calculated percentages. I am trying janitor package but the calculations are not what I am looking for. any ideas how to modify it
df <-data %>%
group_by(season) %>%
adorn_percentages("col") %>%
mutate(across(is.numeric, function(x) round(100*x,2)))


You could divide the
nby the sum of all thenvalues, while grouped by season and transaction. Then, to get it in the shape you want, justpivot_wider.Output: