Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a binary variable based on multiple ordinal variables in R?

I want to use a set of 9 ordinal variables (scale of 0-3) to define a 10th binary variable. The binary variable is defined as:

Of the 9 items, 5 or more are >= 2.

I don’t know how to write code to calculate this other than to list all possible combinations of 5 questions >= 2, such as with the code below. Is there a simpler way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library(tidyverse)
q1 <- c(0,0)
q2 <- c(2,3)
q3 <- c(3,3)
q4 <- c(1,1)
q5 <- c(0,2)
q6 <- c(1,1)
q7 <- c(1,2)
q8 <- c(2,2)
q9 <- c(3,0)

df <- data.frame(q1,q2,q3,q4,q5,q6,q7,q8,q9)

df <- df%>%
  mutate(cutoff = ifelse((q1>=2 & q2>=2 & q3>=2 & q4>=2 & q5>=2) | q2>=2 #etc.
                         ,1,0))



>Solution :

You could try using apply within your ifelse like so:

df$q10 <- ifelse(apply(df, 1, function(x) sum(x >= 2)) >= 5, 1, 0)

Output:

#   q1 q2 q3 q4 q5 q6 q7 q8 q9 q10
# 1  0  2  3  1  0  1  1  2  3   0
# 2  0  3  3  1  2  1  2  2  0   1

The apply(df,1, function(x) sum(x >= 2)) calculates the total number of values greater than or equal to 2 by row (observation), so the first row has 4, and the second row has 5.

apply(df,1, function(x) sum(x >= 2))
# [1] 4 5

Wrapping it in an ifelse statement conditional on >= 5 converts it to the desired dichotomous variable.

Good luck!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading