user14335155
user14335155

Reputation:

Recording if the person has used all response categories across all questionnaire items?

My questionnaire got 30 items, and only 3 response categories (1, 2, and 3) This time, I want to create a new variable that uses records whether or not the person has used all response categories across all questionnaire items. Let me show you:

So if the respondents used multiple responses 1,2,3 throughout the entire questionnaire, the new variable will show either Yes or True. If not, I want it to show No or False.

Upvotes: 1

Views: 38

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388907

In dplyr you can use rowwise with c_across -

library(dplyr)

df <- df %>%
       rowwise() %>%
       mutate(Use = all(1:3 %in% c_across(starts_with('Item')))) %>%
       ungroup

df

#     ID Item1 Item2 Item3 Item4 Item5 Use  
#  <int> <int> <int> <int> <int> <int> <lgl>
#1     1     2     1     3     2     3 TRUE 
#2     2     1     1     1     1     1 FALSE
#3     3     2     2     2     2     2 FALSE
#4     4     3     3     3     3     3 FALSE
#5     5     2     2     2     1     1 FALSE

Upvotes: 0

akrun
akrun

Reputation: 887048

Here is one option

df1$Use <- apply(df1[-1], 1, FUN = function(x) all(1:3 %in% x))

data

df1 <- structure(list(ID = 1:5, Item1 = c(2L, 1L, 2L, 3L, 2L), Item2 = c(1L, 
1L, 2L, 3L, 2L), Item3 = c(3L, 1L, 2L, 3L, 2L), Item4 = c(2L, 
1L, 2L, 3L, 1L), Item5 = c(3L, 1L, 2L, 3L, 1L)), row.names = c(NA, 
-5L), class = "data.frame")

Upvotes: 1

Related Questions