2

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

Fabienne
  • 25
  • 4
  • Please `dput(td_new)`, copy and paste with your question so that is possible to help you. – Duck Jul 05 '20 at 20:10
  • 1
    Have a look at `case_when` function here - https://dplyr.tidyverse.org/reference/case_when.html – Bulat Jul 05 '20 at 20:14
  • For now, `td_new$Hang <- td_new$hang0 = 3` should've been `td_new$Hang <- td_new$hang0 == 3` at least. Same for line 4. You may want to look up `dplyr::case_when`. I think it'll help you. – BellmanEqn Jul 05 '20 at 20:16

1 Answers1

1

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))
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
  • Thank you very much!! Sorry to bother you once again - I know my questions are very stupid... I get a new variable Hang with the correct values in the Console now. But the "spreadsheet" (td_new) in the Data ("Environment") is not updated/changed - why is that? – Fabienne Jul 05 '20 at 20:46
  • When you use the `dplyr` package with the pipe (`%>%`) operator, the original data is not changed. If you want to save the results, you can add, for example, `new_td_new <- ` to the very beginning to assign it to the new object. Or, you can use the fancy `%<>%` operator from `library(magrittr)` package which will automatically assign back. – Ian Campbell Jul 05 '20 at 20:49
  • Thank you again! Will try that tomorrow morning ;). – Fabienne Jul 05 '20 at 20:57