0

Its my first time using R and I have been trying to transfer code across from SPSS to R, the syntax is creating a new variable in R using multiple conditions from multiple variables. The equivalent SPSS syntax is :

COMPUTE OPEN=0.
 
DO IF (APP=1).
   COMPUTE OPEN = 2.
ELSE IF (SCO12 >= 1 AND SCO12 <= 50).
    COMPUTE OPEN = 2.
ELSE IF NOW = 1.
     COMPUTE OPEN = 2.
ELSE IF EWEEK =1.
      COMPUTE OPEN =2.       
ELSE IF ROLL = 1.
      DO IF (AT = 1 or AT = 2).
          COMPUTE OPEN = 2.
      ELSE IF AT = 3.
          COMPUTE OPEN = 1.
      ELSE IF AT = -8.
          COMPUTE OPEN = -8.
      ELSE.
          COMPUTE OPEN = 2.
      END IF.
ELSE IF (APP=-8 OR NOW = -8 OR ROLL = -8 or EWEEK=-8).
      COMPUTE OPEN = -8.
ELSE IF (AGE=16 and (SCO12=97 or SCO12=-9)) .
      COMPUTE OPEN =-8.
ELSE.
      COMPUTE OPEN = 1.
END IF.

The R code i’ve got so far is


OD21 %>% mutate(OPEN = case_when(APP = 1 ~ 2,
                                 (SCO12 >=1 & SCO12 <=50) ~ 2,
                                 NOW = 1 ~ 2,
                                 EWEEK = 1 ~ 2,
                                 ROLL = 1 & (AT = 1 | AT = 2) ~ 2,
                                 ROLL = 1 & AT = 3 ~ 1,
                                 ROLL = 1 & AT = -8 ~ -8)

However I can work out how to continue on from this?

eli-k
  • 10,898
  • 11
  • 40
  • 44
Holly
  • 1
  • 2
  • 1
    Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data and an example of your desired result. – stefan Apr 12 '22 at 15:19
  • Try to write an external function for complicated ifelse conditions and then apply it to the data. – Denny Chen Apr 12 '22 at 15:40

1 Answers1

0

If I understand your SPSS code correctly (not an SPSS user myself)

OD21 <- OD21 %>%
    mutate(OPEN = case_when(
                            # "OPEN" will be == 2
                            APP == 1 ~ 2,
                            (SCO12 >= 1 & SCO12 <= 50) ~ 2,
                            NOW == 1 ~ 2,
                            EWEEK == 1 ~ 2,
                            ROLL == 1 & (AT == 1 | AT == 2) ~ 2,
                            ROLL == 1 & AT != 1 & AT != 2 & AT != 3 & AT != -8 ~ 2,

                            # "OPEN" will be == -8
                            ROLL == 1 & AT == -8 ~ -8,
                            APP == -8 | NOW == -8 | ROLL == -8 | EWEEK == -8 ~ -8,
                            AGE == 16 & (SCO12 == 97 | SCO12 == -9) ~ -8,

                            # "OPEN" will be == 1
                            TRUE ~ 1
                            )

Not tested as I don't have a reproducible example.

  1. In R you check for equivalence between two numbers with == (Not with a single =, which is a different operator).
  2. You can use TRUE ~ at the end to cover all cases that were not covered in previous statements
  3. mutate won't save your dataset unless you re-assign <- the object
Andrea M
  • 2,314
  • 1
  • 9
  • 27