1
votes

I am trying to create a new wage group variable based on a continuous wage variable I already have. This is the code I have and it has worked well on other similar cases:

     df  <-  df %>% mutate(wagegroup = case_when(wage_total < 100 ~ 'below 100 €/m',
    wage_total >= 100 & wage_total <= 546 ~ '100 - 546 €/m', 
    wage_total >= 546,1 & wage_total <= 1000 ~ '565,1 - 1000 €/m', 
    wage_total >= 1000,1 & wage_total <= 1500 ~ '1000,1 - 1500 €/m', 
    wage_total >= 1500,1 & wage_total <= 2000 ~ '1500,1 - 2000 €/m', 
    wage_total >= 2000,1 ~ 'over 2000,1 €/m'))

I get an error code :

x Case 3 (`wage_total < 100 ~ "below 100 \200/m"`) must be a two-sided formula, not a logical vector.

I wonder what is wrong here? I am just a beginner with Rstudio, so I would very much appreciate the help :)

1
Looks like something may be amiss with the ,1 that appear in the last 4 lines. - tomasu
In order for us to help you, please edit your question to include a reproducible example. For example, to produce a minimal data set, you can use head(), subset(), or the indices. Then use dput() to give us something that can be put in R immediately. Also, please make sure you know what to do when someone answers your question. More info can be found at StackOverflow's help center. Thank you! - iamericfletcher

1 Answers

2
votes

You've got what appear to be some extraneous characters in your code. I tried to take my best guess at what you are trying to do:

df <- data.frame(wage_total = c(100, 200, 300, 500, 600, 1020, 1038))

df  <-  df %>% mutate(wagegroup = case_when(wage_total < 100 ~ 'below 100 €/m',
                                            wage_total >= 100 & wage_total <= 546 ~ '100 - 546 €/m', 
                                            wage_total >= 546 & wage_total <= 1000 ~ '565 - 1000 €/m', 
                                            wage_total >= 1000 & wage_total <= 1500 ~ '1000 - 1500 €/m', 
                                            wage_total >= 1500 & wage_total <= 2000 ~ '1500 - 2000 €/m', 
                                            wage_total >= 2000 ~ 'over 2000 €/m'))

df
#   wage_total       wagegroup
# 1        100   100 - 546 €/m
# 2        200   100 - 546 €/m
# 3        300   100 - 546 €/m
# 4        500   100 - 546 €/m
# 5        600  565 - 1000 €/m
# 6       1020 1000 - 1500 €/m
# 7       1038 1000 - 1500 €/m