With the following code I assign a quantile rank y (from 1 to 4) for every value of x.
df$y <- ntile(df$x, 4)
Then, I would like to have four separate columns for absolute frequency count of every quantile rank, grouped also by variable z. With the following code, it does the calculation but I get all calculations in the same column.
df <-
df %>%
group_by(z, y) %>%
mutate(Freq = n())
example:
z y(quartile) n_quartile_4 n_quartile 3 n_quartile 2
1 4 2 1 0
1 3 2 1 0
1 4 2 1 0
2 2 0 0 3
2 2 0 0 3
2 2 0 0 3