0

I have a scale with six items, Q1 to Q6. Response ranges from 1 to 5 for each item. I would like to compute a score conforming to this rule:

"If you scored a 4 or 5 ("often" or "very often") on at least 4 of those statements... "

I have a relatively hard time to program this. Do you have any tips? G.

George GL
  • 29
  • 3
  • 1
    Welcome to Stack Overflow! It would help if you can provide a reprex: https://stackoverflow.com/help/minimal-reproducible-example – Matt Mar 11 '20 at 14:39
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 11 '20 at 14:50

2 Answers2

0

Without any example data it is hard to tell which structure you have, but you could loop through the answers and check within every iteration if your condition (answer equals "often" or "very often") is true. If so, set a counter +1. At the end the counter indicates on how many of those questions the recipent answered with "often" or "very often".

satesrah
  • 93
  • 8
0

Hard to do without any coding example but this is an example of what you could do:

library(data.table)

dt <- data.table(ID = 1:5, Q1 = c(1,4,2,2,3), Q2 = c(4,4,3,5,3), Q3 = c(5,4,1,5,5), 
                Q4 = c(5,3,4,3,3), Q5 = c(5,5,2,5,4), Q6 = c(2,1,4,4,5))

dt[, Qx := ifelse(rowSums(dt[, -1] >= 4) >= 4, T, F)]

Which results to this:

enter image description here

koolmees
  • 2,725
  • 9
  • 23
  • Thank you! this ought to do it. TRUE / FALSE is great. You saved me hours of worktime. Virtual beer's on me. – George GL Mar 13 '20 at 17:41