Setting new Variable with case_when condition

Hallo, i have an SPSS syntax which worked in SPSS perfectly. For further Regression Models I changed to R Studio now, but I didn't manage until now to convert the SPSS Syntax into an RScrpit. Maybe someone can help me here. My task is, I have the Variable CTQ_EM, which I want to convert into CTQ_EM_SG, and this variable should have the value 1 if it is <8, value 2 if it is between 9-12, value 3 between 13-15, and value 4 >= 16.
So here is the original SPSS Syntax:
IF (CTQ_EM <= 8) CTQ_EM_SG=0.
EXECUTE.
IF(CTQ_EM >=9) CTQ_EM_SG=1
EXECUTE.
IF (CTQ_EM >= 13) CTQ_EM_SG=2.
EXECUTE.
IF (CTQ_EM >= 16) CTQ_EM_SG=3.
EXECUTE.

i have tried different codes, but none of them have worked yet, maybe someone has an idea ?thank you very much in advance!
1)
Df %>% mutate(CTQ_emo_abuse_SG=case_when(Df,CTQ_emo_abuse <= 8 ~ '1', between(CTQ_emo_abuse, 9, 12) ~ '2', between(CTQ_emo_abuse, 13, 15) ~ '3', CTQ_emo_abuse >=16 ~ 4))
—>Error in mutate():
:information_source: In argument: CTQ_emo_abuse_SG = case_when(...).
Caused by error in case_when():
! Failed to evaluate the left-hand side of formula 1.
Caused by error:
! object 'CTQ_emo_abuse' not found

  1. Df <- mutate (Df, CTQ_emo_abuse_SG = case_when(Df$CTQ_emo_abuse <= 8 ~ 1,Df$CTQ_emo_abuse >=9 ~ 2, Df$CTQ_emo_abuse >=13 ~ 3, Df$CTQ_emo_abuse >=16 ~ 4))

—> CTQ_emo_abuse_SG must be size 209 or 1, not 0.

Your example is confusing because it is not clear if you want to look at the value of CTQ_EM or CTQ_emo_abuse. Here is an example using CTQ_EM.

Df <- data.frame(CTQ_EM = c(5,17,12,15))
library(dplyr)

Df <- Df |> mutate(CTQ_EM_SG = case_when(
  CTQ_EM <= 8 ~ '1',
  between(CTQ_EM, 9, 12) ~ '2',
  between(CTQ_EM, 13, 15) ~ '3',
  CTQ_EM >= 16 ~ '4'
))
Df
#>   CTQ_EM CTQ_EM_SG
#> 1      5         1
#> 2     17         4
#> 3     12         2
#> 4     15         3

Created on 2023-04-18 with reprex v2.0.2

hey, thank you very much, sorry there where two different names yes ! I tried you syntax for CTQ_emo_abuse.. but it still shows errors.. ?

Df <- Df |> mutate(CTQ_emo_abuse_SG = case_when(

  •            CTQ_emo_abuse <= 8 ~ '1',
    
  •            between(CTQ_emo_abuse, 9, 12) ~ '2',
    
  •            between(CTQ_emo_abuse, 13, 15) ~ '3',
    
  •            CTQ_emo_abuse >= 16 ~ '4'
    
  •          ))
    

Error in mutate():
:information_source: In argument: CTQ_emo_abuse_SG = case_when(...).
Caused by error in case_when():
! Failed to evaluate the left-hand side of formula 2.
Caused by error in between():
! Can't combine x <tbl_df> and left .
Run rlang::last_trace() to see where the error occurred.

rlang::last_trace()
<error/dplyr:::mutate_error>
Error in mutate():
:information_source: In argument: CTQ_emo_abuse_SG = case_when(...).
Caused by error in case_when():
! Failed to evaluate the left-hand side of formula 2.


Backtrace:

  1. ├─dplyr::mutate(...)
  2. ├─dplyr:::mutate.data.frame(...)
  3. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
  4. │ ├─base::withCallingHandlers(...)
  5. │ └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
  6. │ └─mask$eval_all_mutate(quo)
  7. │ └─dplyr (local) eval()
  8. ├─dplyr::case_when(...)
  9. │ └─dplyr:::case_formula_evaluate(...)
  10. │ ├─base::withCallingHandlers(...)
  11. │ └─rlang::eval_tidy(pair$lhs, env = default_env)
  12. ├─dplyr::between(CTQ_emo_abuse, 9, 12)
  13. │ └─vctrs::vec_cast_common(!!!args)
  14. ├─vctrs (local) <fn>()
  15. │ └─vctrs::vec_default_ptype2(...)
  16. │ ├─base::withRestarts(...)
  17. │ │ └─base (local) withOneRestart(expr, restarts[[1L]])
  18. │ │ └─base (local) doWithOneRestart(return(expr), restart)
  19. │ └─vctrs::stop_incompatible_type(...)
  20. │ └─vctrs:::stop_incompatible(...)
  21. │ └─vctrs:::stop_vctrs(...)
  22. │ └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)
  23. │ └─rlang:::signal_abort(cnd, .file)
  24. │ └─base::signalCondition(cnd)
  25. └─dplyr (local) <fn>(<vctrs__2>)
    Caused by error in between():
    ! Can't combine x <tbl_df> and left .

Backtrace:

  1. ├─dplyr::mutate(...)
  2. ├─dplyr:::mutate.data.frame(...)
  3. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
  4. │ ├─base::withCallingHandlers(...)
  5. │ └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
  6. │ └─mask$eval_all_mutate(quo)
  7. │ └─dplyr (local) eval()
  8. ├─dplyr::case_when(...)
  9. │ └─dplyr:::case_formula_evaluate(...)
  10. │ ├─base::withCallingHandlers(...)
  11. │ └─rlang::eval_tidy(pair$lhs, env = default_env)
  12. └─dplyr::between(CTQ_emo_abuse, 9, 12)
    Run rlang::last_trace(drop = FALSE) to see 5 hidden frames.

any ideas on that ..?

I have not seen that error before. Please post the output of

dput(head(Df))

Put a line with three back ticks just before and after the output, like this
```
pasted dput() output goes here
```

thank you for the try, same answer as above came.. but I'll try posting in other communities or if someone has a different suggestion ?

We need to see the output of the dput() function here on the forum so we can run your code using your data. Please post it here.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.