0

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

Andreas
  • 3
  • 1

1 Answers1

0

Is this sufficient? You can use the three logical vectors to subset your data.

If you want more details it would help to have some of your data to work with. (help)

lev <- c("Never", "Never", "Never", "Rarely", "Sometimes", "Often", "Very Often")

set.seed(1)

X <- factor(sample(c(lev, "Never"), 15, replace=TRUE), levels=unique(lev))
Y <- factor(sample(lev, 15, replace=TRUE), levels=unique(lev))

None <-   X %in% c("Never") & 
          Y %in% c("Never")

Mild <-   X %in% c("Rarely", "Sometimes", "Often", "Very Often") &
          Y %in% c("Never")

Severe <- Y %in% c("Rarely", "Sometimes", "Often", "Very Often")

If you want to combine the logical vectors into a single factor vector it can be done something like this:

Z0 <- rbind(None, Mild, Severe)
z <- which(Z0, arr.ind=TRUE)
Z <- factor(rownames(z), levels=rownames(Z0))

data.frame(X, Y, Z)
#             X          Y      Z
# 1       Never     Rarely Severe
# 2       Never      Often Severe
# 3   Sometimes Very Often Severe
# 4       Never      Never   None
# 5       Never      Often Severe
# 6       Never Very Often Severe
# 7       Never      Never   None
# 8       Often  Sometimes Severe
# 9       Often      Never   Mild
# 10      Never      Never   None
# 11      Never      Never   None
# 12      Never      Never   None
# 13      Often      Never   Mild
# 14     Rarely Very Often Severe
# 15 Very Often      Never   Mild
AkselA
  • 8,153
  • 2
  • 21
  • 34
  • Thank you so much Aksel for your help; it works! Unfortunately, I am not allowed to share the data. – Andreas May 08 '18 at 08:44
  • @Andreas: No problem if you don't want to share the exact data. The idea is more to have example data that shares relevant characteristics with the original data. In the help link I posted above there is an answer concerning 'anonymization' of private data. It might be worth a read. – AkselA May 08 '18 at 09:42