Reputation:
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
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
Reputation: 887048
Here is one option
df1$Use <- apply(df1[-1], 1, FUN = function(x) all(1:3 %in% x))
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