Search code examples
rif-statementspssrecode

Recode continuous variable in R based on conditions


I want to "translate" a syntax written in SPSS into R code but am a total beginner in R and struggling to get it to work.

The SPSS syntax is

DO IF  (Geschlecht = 0).
RECODE hang0 (SYSMIS=SYSMIS) (Lowest thru 22.99=0) (23 thru 55=1) (55.01 thru Highest=2)  
    INTO Hang.
ELSE IF (Geschlecht = 1).
RECODE hang0 (SYSMIS=SYSMIS) (Lowest thru 21.99=0) (22 thru 54=1) (54.01 thru Highest=2) 
    INTO Hang.
END IF.

I have installed the "car"-package in R but I neither get the "range" recoding to work (I have tried

td_new$Hang <- recode(td_new$hang0, "0:22.99=0; 23:55=1; else=2")

nor do I manage to work with the if-else-function. My last attempt was

if(td_new$Geschlecht == 0){
  td_new$Hang <- td_new$hang0 = 3
} else if (td_new$Geschlecht == 1) {
  td_new$Hang <- td_new$hang0 = 5)
} else
  td_new$hang0 <- NA

(this was without the recoding, just to test the if-else function).

Would be very happy if someone helped!

Thanks a lot in advance :)!

Sorry, edited to add: The data structure looks as follows:

Geschlecht  hang0
0           15
1           45
1            7
0           11

And I want to recode hang0 such that for boys (Geschlecht = 0): all values < 23 = 0, values between 23 and 55 = 1, all values > 55 = 2 and for girls (Geschlecht = 1): all values < 22 = 0, values between 23 and 54 = 1, all values > 54 = 2


Solution

  • Here's an approach with case_when:

    library(dplyr)
    td_new %>%
      mutate(Hang = case_when(Geschlecht = 0 & hang0 < 23 ~ 0,
                              Geschlecht = 0 & hang0 >= 23 &  hang0 < 55 ~ 1,
                              Geschlecht = 0 & hang0 >= 55 ~ 2,
                              Geschlecht = 1 & hang0 < 22 ~ 0,
                              Geschlecht = 1 & hang0 >= 22 &  hang0 < 54 ~ 1,
                              Geschlecht = 1 & hang0 >= 54 ~ 2,
                              TRUE ~ NA_real_))
    #  Geschlecht hang0 Hang
    #1          0    15    0
    #2          1    45    1
    #3          1     7    0
    #4          0    11    0
    

    The final line is there to catch NAs.

    Data

    td_new <- structure(list(Geschlecht = c(0L, 1L, 1L, 0L), hang0 = c(15L, 45L, 7L, 11L)), class = "data.frame", row.names = c(NA, -4L))