lundi 7 mai 2018

R: Multiple conditions to categorise two survey questions (factors) into one factor with levels None/Mild/Severe

I have two factors X and Y with 5 levels (5 point Likert scale)

"[1] Never"      "[2] Rarely"     "[3] Sometimes"  "[4] Often"      "[5] Very Often"

Depending on both answers, I want to create a new factor Z with the following coding:

None: X = Never AND Y = Never

Mild: X = Rarely, Sometimes, Often, or Very Often AND Y = Never

Severe: Y = Rarely, Sometimes, Often, or Very Often

I tried many different multiple conditions but none worked.

Here is one of them:

Z <- c("None", "Mild", "Severe")
    factor(Z)
    levels(Z) <- c("None", "Mild", "Severe")
    if (!is.na((X == 1) && (Y == 1))) {
      Z == "None"
      } else if (!is.na((X != 1) && (Y == 1))) {
        Z == "Mild"
        } else (!is.na((X != 1) && (Y != 1))) {
          Z == "Severe"
          }

The error message is:

Error: unexpected '{' in:
"    Z== "Mild"
    } else (!is.na((X != 1) && (Y != 1))) {"
>       Z == "Severe"
[1] FALSE FALSE  TRUE
>       }
Error: unexpected '}' in "      }"
> 

The sample consists of about 4000 people and I want to know which participant is in which category (e.g. depression) according to his or her ratings on questions X and Y.

I am still an R beginner so I am grateful for your help!

All best

Aucun commentaire:

Enregistrer un commentaire