Marco
Marco

Reputation: 2797

How to effectively split numeric column into groups with dplyr?

I have a numeric column and try to use the case_when function. I know that ntile() can split numerics into equal groups, like

# data
data = data.frame(id=c(1,1,1,2,2,2,3,3,3,4,4,4), time=seq(1:3), x=seq(from=10, to=120, by=10))

# automatic grouping works
data%>%
  mutate(x_group = ntile(x, n=4))

but I'm would like to specify these groups by hand:

# manual grouping fails 
data %>%
  mutate(x_group = case_when(x < 30 ~ 1,
                               30 <= x < 50 ~ 2,
                               50 <= x < 70 ~ 3,
                               70 <= x ~ 4))

I guess the problem might be in the combination of the conditionals?

Upvotes: 1

Views: 933

Answers (1)

mnist
mnist

Reputation: 6956

You have to specify two separate logical comparisons in R

data %>%
  mutate(x_group = case_when(x < 30 ~ 1,
                             30 <= x & x < 50 ~ 2,
                             50 <= x & x < 70 ~ 3,
                             70 <= x ~ 4))

Upvotes: 2

Related Questions