Home > Enterprise >  Use values of variables 1,2,3 as condition to set the value of variable 4
Use values of variables 1,2,3 as condition to set the value of variable 4

Time:09-11

So I basically have a (badly designed) survey, with multiple item batteries where for each item, multiple replies are possible and therefore there are multiple variables per item which can be either 0 or 1 depending on whether that answer was clicked. There is also a "don't know" answer, which again, has its own variable.

Due to the optional nature of these answers some respondents who should've chosen the "don't know" category chose none at all, I want to rectify this (I know, questionable in terms of quality). I want to code an ifelse statement that recodes the dk variable, let's call it variable 4, to 1 if the previous variables 1,2 and 3 have the value 0 within one line/case in the dataset.

I have tried the following, sorry for the german variable names:

ifelse(df_c$F4T1_SQ001_SQ001_pc_privat==0 & df_c$F4T1_SQ001_SQ002_pc_mathematik==0 & df_c$F4T1_SQ001_SQ003_pc_deutsch==0 & df_c$F4T1_SQ001_SQ004_pc_sachunterricht==0 & df_c$F4T1_SQ001_SQ005_pc_englisch==0 & df_c$F4T1_SQ001_SQ006_pc_sport==0 & df_c$F4T1_SQ001_SQ007_pc_ethik==0, df_c$F4T1_SQ001_SQ008_pc_nicht==1, df_c$F4T1_SQ001_SQ008_pc_nicht==0)

sadly this only returned statements regarding the condition being TRUE or not and I am unsure if it actually properly checked for the condition. Any input would be appreciated, including easier ways to solve this. Thanks in advance!

CodePudding user response:

Please, try this:

if (!require("tidyverse")) { install.packages("tidyverse") }
library(tidyverse)
df_c %<>%
    mutate(F4T1_SQ001_SQ008_pc_nicht = ifelse(F4T1_SQ001_SQ001_pc_privat | 
                                              F4T1_SQ001_SQ002_pc_mathematik | 
                                              F4T1_SQ001_SQ007_pc_ethik, 0, 1)

  • Related