Reputation: 83
I have a data frame named cst
with columns country
, ID
, and age
. I want to make bins for age
(divide all ID's into deciles or quartiles) for each separate country. I used this way:
cut(cst[!is.na(cst$age), "age"], quantile(cst["age"], probs = seq(0,1,0.1), na.rm = T))
However, it makes bins for all data frame, but I need for each country separately.
Could you help me?
Upvotes: 0
Views: 2515
Reputation: 410
All you need to do is to apply a subset before using the cut
. It also does not employ the dplyr
library.
for (c in unique(as.list(cst$country))) {
sub <- subset(cst, country == c)
cut(sub[!is.na(sub$age), "age"], quantile(sub["age"], probs = seq(0,1,0.1), na.rm = T))
}
Upvotes: 0
Reputation: 2535
I'd try with a dplyr
solution, this would look someithing like this:
library(dplyr)
cst2 <- cst %>%
group_by(country) %>%
mutate(
bin = cut(age, quantile(age, probs=seq(0,1,0.1), na.rm=TRUE))
) %>%
ungroup()
Upvotes: 2