lundi 22 août 2016

Multiple condition if statement in r [duplicate]

This question already has an answer here:

I am hoping that someone might be able to help me to figure out how to do a multiple condition if statement in r.

Specifically, I asked a question (are you doing x?) in two surveys. If participants said yes, they get a 1. If they have an NA, it could be because they did not complete that survey or because they did complete that survey and were not doing X. To help address this, I have two variables (one per survey) that indicate whether the participant completed the survey (survey1complete=1 or survey2complete=1). I want to find a way to do the following:

If ((survey1complete==1 | survey2complete==1) & (doingx==1)) {newvariable=1)

If ((survey1complete==1 | survey2complete==1) & (is.na(doingx))) {newvariable=0}

ELSE {newvariable="NA"}

When I have tried to code this, I am struggling because I get an error message saying "the condition has length > 1 and only the first element will be used."

It seems like I may be running into a problem not addressed in other threads with my use of both an or and and & statement, as my attempts to follow other threads have left me stuck. My apologies if I wasn't able to find the appropriate thread and it does exist.

Aucun commentaire:

Enregistrer un commentaire