Giedrius Urbonas
Giedrius Urbonas

Reputation: 83

How to create bins in R

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

Answers (2)

Iago Carvalho
Iago Carvalho

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

snaut
snaut

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

Related Questions