1

I was wondering whether using the | operator would do the same thing as the 'any' function in SPSS?

For example, I have tried to rewrite this SPSS code into R but I'm not getting the same answers:

SPSS:

IF ((any (age, 11, 12, 13, 21, 42, 33)) AND (income2 = 
1)) inc_age = 1.

R:

selected_18_19 <- 2017_18[ which(2017_18$age 
== 
11 | 2017_18$age == 12 | 2017_18$age == 13 
| 2017_18$age == 21 | 2017_18$age == 42 | 
2017_18$age == 33 & (2017_18$income == 1)),]

I am getting output but the frequencies on the selected cases are different.

Grateful for any suggestions.

Cheers.

  • 1
    Welcome to SO! Please take a moment to read about how to post R questions: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – YOLO Feb 26 '20 at 16:53
  • 3
    `2017_18` is not a valid name in the way you are using it--you would need to use back-ticks `\`2017_18\`$age` in this case. an easier way would be similar to the spss code: `any(age %in% c(11, 12, 13, ...))` but i don't read spss code, so I can't be sure – rawr Feb 26 '20 at 16:53

1 Answers1

1

Here is a tidyverse solution:

library(dplyr)

selected_18_19 <- `2017_18` %>%
  filter((`2017_18`$age %in% c(11,12,13,21,42,33)) & `2017_18`$income == 1) %>%
  mutate(inc_age = 1)

You would definitely benefit from first renaming your dataframe so that it starts with a character, i.e.:

df_2017_18 <- as.data.frame(`2017_18`)
Matt
  • 7,255
  • 2
  • 12
  • 34