-1

I've read in my SPSS file in R and want to recode a new variable if such and such assumptions are made. To be specific:

I want to turn my spssdata_sub$gest variable into a new variable if the following the conditions are met:

spssdata_sub$indusert != 2 & spssdata_sub$ivf != 1 & spssdata_sub$leie != 3 & spssdata_sub$svkompl_II != 7 & spssdata_sub$svkompl_II != 2 & spssdata_sub$svkompl_II != 1

Anyone here who can help me with a code?

lisah
  • 186
  • 1
  • 9
Gunn-Helen Moen
  • 115
  • 2
  • 10
  • `spssdata_sub$gest <- ifelse(spssdata_sub$indusert != 2 & spssdata_sub$ivf != 1 & spssdata_sub$leie != 3 & spssdata_sub$svkompl_II != 7 & spssdata_sub$svkompl_II != 2 & spssdata_sub$svkompl_II != 1, newvalue, spssdata_sub$gest)` – IceCreamToucan Feb 19 '18 at 13:41
  • I'm not sure I understand this code. If I run this I just get a new variable with NA values. Sorry, I'm pretty new to R, and trying to learn! – Gunn-Helen Moen Feb 19 '18 at 13:45
  • Can you maybe provide a reproducible example (see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for instructions)? – lisah Feb 19 '18 at 13:51
  • And maybe an addition to the comment of @Renu: You have to change *newvalue* to an actual value. – lisah Feb 19 '18 at 13:54
  • Thanks, what I want is the values of spssdata_sub$gest to be the same, but if spssdata_sub$indusert != 2 & spssdata_sub$ivf != 1 & spssdata_sub$leie != 3 & spssdata_sub$svkompl_II != 7 & spssdata_sub$svkompl_II != 2 & spssdata_sub$svkompl_II != 1 Then I want them left out of the new variable – Gunn-Helen Moen Feb 19 '18 at 13:57
  • To clarify it should be or not &. I dont want it in the new variable if any of criteria are met. – Gunn-Helen Moen Feb 19 '18 at 13:59
  • Sorry, I'm not sure whether I understand you correctly, but `ifelse()` works as follows: `ifelse(condition, yes, no)`, with `yes` meaning (i.e., returning) the value if the condition is met and `no` meaning the value if the condition isn't met. – lisah Feb 19 '18 at 14:05
  • What I'm looking to do is to exclude individuals which has either of the following from my dataset. if either spssdata_sub$indusert = 2 spssdata_sub$ivf = 1 spssdata_sub$leie = 3 spssdata_sub$svkompl_II = 7 spssdata_sub$svkompl_II = 2 spssdata_sub$svkompl_II = 1 Then I want all thouse individuals out. I only want to have spssdata_sub$gest for individuals which has none of these.. – Gunn-Helen Moen Feb 19 '18 at 14:07

2 Answers2

0

Does one of the following codes work for you?

Either this adapted version of Renu's solution

spssdata_sub$gest <- ifelse(spssdata_sub$indusert != 2 & spssdata_sub$ivf != 1 & spssdata_sub$leie != 3 & spssdata_sub$svkompl_II != 7 & spssdata_sub$svkompl_II != 2 & spssdata_sub$svkompl_II != 1, spssdata_sub$gest, NA)

or this code for filtering observations:

library(dplyr)
spssdata_sub_new <- spssdata_sub %>% 
filter(indusert != 2 & ivf != 1 & leie != 3 & svkompl_II != 7 & svkompl_II != 2 & ssvkompl_II != 1)
lisah
  • 186
  • 1
  • 9
  • 1
    No, the first one only return a list of NA, I think a problem there might be that it is saying and to all the criteria? I need an OR criteria, as individuals dont need to have more than one to be excluded. I've tried | insted of &, but that does not help. On #2 I just get an error. – Gunn-Helen Moen Feb 19 '18 at 14:26
  • If it's OR then it should be `|` instead of `&`. – lisah Feb 19 '18 at 14:29
0

One way is the following, if you really mean either one of the conditions

Mynewdata <- dplyr::filter(spssdata, indusert != 2, ivf != 1, leie != 3,
             svkompl_II != 7 & svkompl_II != 2 & svkompl_II != 1)

only keeps entries that are neither, or putting it the other way exludes entries that have either indusert = 2 or ivf = 1 etc... one of the condition is enough to exclude it.

add-on: or something also like that:

Mynewdata <- dplyr::filter(spssdata, indusert != 2, ivf != 1, leie != 3,
             !(svkompl_II %in% c(7,2,1))
R. Prost
  • 1,958
  • 1
  • 16
  • 21