I have a panel data set with id, year and numerical column A. I want to add a column B with a factor variable indicating to which quantile group each row belongs per year, with either "lowest 33%", "33-67%" or "highest 33%".
I played around with dplyrs group_by(year) and the quantile() function but this just gave me back the value of the quantile. I also found the function quantcut(), that is supposed to be doing something similar to what I need, but I have had no success.
Data:
df <- data.frame(id = c(1, 1, 1, 2, 2, 2, 3, 3, 3,
4, 4, 4, 5, 5, 5),
year = c(2000, 2005, 2010, 2000, 2005, 2010,
2000, 2005, 2010, 2000, 2005, 2010)
A = c(0, 3000, 1000, 5000, 0, 2000, 2000,
1000, 0, 0, 1000, 7000))
This is what I tried last which didn't give me anything useful:
library(tidyverse)
library(gtools)
df <- df %>%
mutate(B = quantcut(A, probs=seq(0.33, 0.67, 1)))