зигзагообразный паттерн в матрице вопросов - PullRequest
0 голосов
/ 01 февраля 2020

Я работаю над данными опроса. Он задает несколько матричных вопросов об уровне удовлетворенности респондентов несколькими пунктами. Ниже приведен пример.

q1: насколько вы удовлетворены предметом А? «очень доволен» «несколько доволен» несколько недоволен «очень недоволен»

q2: насколько вы удовлетворены предметом B? «очень доволен» «несколько доволен» несколько недоволен «очень недоволен»

q3: насколько вы удовлетворены предметом C? "очень доволен" "несколько доволен" несколько недоволен "очень недоволен"

q4: насколько вы удовлетворены пунктом D? «очень доволен» «несколько доволен» несколько недоволен «очень недоволен»

Данные выглядят так:

df <- data.frame(q1 = c("Very satisfied", "Somewhat satisfied", "Very satisfied", "Very satisfied", "Somewhat satisfied", "Very dissatisfied", "Very satisfied", "Very dissatisfied", "Very dissatisfied", "Somewhat dissatisfied"),
                 q2 = c("Somewhat satisfied", "Very satisfied", "Somewhat satisfied", "Very satisfied", "Very satisfied", "Somewhat dissatisfied", "Somewhat dissatisfied", "Somewhat dissatisfied", "Very dissatisfied", "Very dissatisfied"),
                 q3 = c("Very satisfied", "Somewhat satisfied", "Very satisfied", "Very satisfied", "Somewhat satisfied", "Very dissatisfied", "Very satisfied", "Very dissatisfied", "Very dissatisfied", "Somewhat dissatisfied"),
                 q4 = c("Somewhat satisfied", "Very satisfied", "Somewhat satisfied", "Very satisfied", "Very satisfied", "Somewhat dissatisfied", "Somewhat dissatisfied", "Somewhat dissatisfied", "Very dissatisfied", "Very dissatisfied"))

                      q1                    q2                    q3                    q4
1         Very satisfied    Somewhat satisfied        Very satisfied    Somewhat satisfied
2     Somewhat satisfied        Very satisfied    Somewhat satisfied        Very satisfied
3         Very satisfied    Somewhat satisfied        Very satisfied    Somewhat satisfied
4         Very satisfied        Very satisfied        Very satisfied        Very satisfied
5     Somewhat satisfied        Very satisfied    Somewhat satisfied        Very satisfied
6      Very dissatisfied Somewhat dissatisfied     Very dissatisfied Somewhat dissatisfied
7         Very satisfied Somewhat dissatisfied        Very satisfied Somewhat dissatisfied
8      Very dissatisfied Somewhat dissatisfied     Very dissatisfied Somewhat dissatisfied
9      Very dissatisfied     Very dissatisfied     Very dissatisfied     Very dissatisfied
10 Somewhat dissatisfied     Very dissatisfied Somewhat dissatisfied     Very dissatisfied

Я должен выяснить все эти наблюдения по следующей схеме:

case1

если q1 = "Очень доволен" и q2 = "Отчасти удовлетворен" и q3 = "очень доволен" и q4 = "несколько доволен"

case2

или q1 = "Очень доволен" и q2 = "несколько недоволен" и q3 = «очень доволен» и q4 = «несколько недоволен»

case3

или q1 = "Очень доволен" и q2 = "очень недоволен" и q3 = "очень доволен" и q4 = "очень недоволен"

I can find this pattern using below command. However, since I have to do this for several matrices and number of questions in each matrix varies, I wonder if anyone knows an easy way of doing this.


df %>%
  mutate(case1 = ifelse((q1 %in% "Very satisfied" & q2 %in% "Somewhat satisfied" & q3 %in% "Very satisfied" & q4 %in% "Somewhat satisfied"), TRUE, FALSE),
         case2 = ifelse((q1 %in% "Very satisfied" & q2 %in% "Somewhat dissatisfied" & q3 %in% "Very satisfied" & q4 %in% "Somewhat dissatisfied"), TRUE, FALSE),
         case3 = ifelse((q1 %in% "Very satisfied" & q2 %in% "Very dissatisfied" & q3 %in% "Very satisfied" & q4 %in% "Very dissatisfied"), TRUE, FALSE),
         zigzag = ifelse((case1 %in% TRUE | case2 %in% TRUE | case3 %in% TRUE), 1, 0)
         )


                      q1                    q2                    q3                    q4 case1 case2 case3 zigzag
1         Very satisfied    Somewhat satisfied        Very satisfied    Somewhat satisfied  TRUE FALSE FALSE      1
2     Somewhat satisfied        Very satisfied    Somewhat satisfied        Very satisfied FALSE FALSE FALSE      0
3         Very satisfied    Somewhat satisfied        Very satisfied    Somewhat satisfied  TRUE FALSE FALSE      1
4         Very satisfied        Very satisfied        Very satisfied        Very satisfied FALSE FALSE FALSE      0
5     Somewhat satisfied        Very satisfied    Somewhat satisfied        Very satisfied FALSE FALSE FALSE      0
6      Very dissatisfied Somewhat dissatisfied     Very dissatisfied Somewhat dissatisfied FALSE FALSE FALSE      0
7         Very satisfied Somewhat dissatisfied        Very satisfied Somewhat dissatisfied FALSE  TRUE FALSE      1
8      Very dissatisfied Somewhat dissatisfied     Very dissatisfied Somewhat dissatisfied FALSE FALSE FALSE      0
9      Very dissatisfied     Very dissatisfied     Very dissatisfied     Very dissatisfied FALSE FALSE FALSE      0
10 Somewhat dissatisfied     Very dissatisfied Somewhat dissatisfied     Very dissatisfied FALSE FALSE FALSE      0


** Заранее спасибо! **

1 Ответ

0 голосов
/ 02 февраля 2020

Для примера, который вы предложили, поскольку условия для q1 и q3 одинаковы при тестировании с учетом конкретного случая, вы можете получить зигзагообразный результат с помощью:

df[with(df, q1 == "Very satisfied" & 
            q2 == q4 & 
            q3 == "Very satisfied" & 
     q4 %in% c( "Very dissatisfied", "Somewhat dissatisfied", "Somewhat satisfied") ), ]

r2evans уже указал на избыточность использования ifelse. Если бы вы хотели получить числовое значение c для результата zigzag, которое вы могли бы использовать более компактно, просто:

zigzag = as.numeric( case1 | case2 | case3 ) # since 1 == TRUE
...