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

Time:12-31

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?

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))



CodePudding user response:

I would propose these two steps:

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$counter <- rowSums(df>=2)
df$cutoff <- ifelse(df$counter>=5,1,0)
df
#>   q1 q2 q3 q4 q5 q6 q7 q8 q9 counter cutoff
#> 1  0  2  3  1  0  1  1  2  3       4      0
#> 2  0  3  3  1  2  1  2  2  0       5      1

Created on 2022-12-30 with reprex v2.0.2

CodePudding user response:

You could try using apply or rowSums within your ifelse like so:

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

# or

df$q10 <- ifelse(rowSums(df >= 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

Both the apply(df,1, function(x) sum(x >= 2)) and ifelse(rowSums(df >= 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!

  • Related