1

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 :)

  • 2
    Looks like something may be amiss with the `,1` that appear in the last 4 lines. – tomasu Mar 18 '21 at 18:07
  • 1
    In order for us to help you, please edit your question to include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-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](https://stackoverflow.com/help/someone-answers). More info can be found at StackOverflow's [help center](https://stackoverflow.com/help). Thank you! – iamericfletcher Mar 18 '21 at 18:09

1 Answers1

1

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
David J. Bosak
  • 1,081
  • 9
  • 20
  • 1
    It is possible to simplify the case_when condition... case_when(wage_total < 100 ~ 'below 100 €/m', wage_total < 546 ~ '100 - 545 €/m', wage_total < 1000 ~ '546 - 999 €/m', wage_total < 1500 ~ '1000 - 1499 €/m', wage_total < 2000 ~ '1500 - 1999 €/m', TRUE ~ 'over 1999 €/m')) – abreums Mar 18 '21 at 18:21