rif-statementmultiple-conditions

Calculate a score depending on multiple conditions in R


I'm dealing with a dataframe showing answers to a questionnaire by several participants. A certain question can have many correct answers and the data look like this:

a <- c(0, 0, 1, 0, 1)
b <- c(0, 1, 1, 1, 0)
c <- c(0, 0, 0, 0, 0)
d <- c(1, 1, 1, 1, 0)
ID <- c("001", "002", "003", "004", "005")
df <- data.frame(ID=ID, a=a, b=b, c=c, d=d)
df
 ID a b c d
001 0 0 0 1
002 0 1 0 1
003 1 1 0 1
004 0 1 0 1
005 1 0 0 0

a, b, c, and d are different answering options to the same question. For instance, participant 001 only ticked option d, while participant 002 chose b and d.

Let's say that only a and b are correct answers, while c and d are wrong. I want to compute a score indicating how knowledgeable is each participant. For each correct answer (a or b), the participant must be given 1 point. For each wrong answer (c or d), the participant loses 0.5 points.

So, I need to add a new variable "score" to the dataframe, which is the sum of the points assigned to each participants. Something like - if "a == 1" <- 1, else if ..., but I'm bad at building complex if/else statements.

Thank you


Solution

  • Create a score function to compute the scores.
    rowSums will add the right answers and the wrong answers separately. The wrong answers sums are multiplied by 0.5. Then the two results are added and returned to caller. All in a one-liner.

    a <- c(0, 0, 1, 0, 1)
    b <- c(0, 1, 1, 1, 0)
    c <- c(0, 0, 0, 0, 0)
    d <- c(1, 1, 1, 1, 0)
    ID <- c("001", "002", "003", "004", "005")
    df <- data.frame(ID=ID, a=a, b=b, c=c, d=d)
    
    score <- function(x, pos = c("a", "b"), neg = c("c", "d")) {
      rowSums(x[pos]) - 0.5*rowSums(x[neg])
    }
    score(df)
    #> [1] -0.5  0.5  1.5  0.5  1.0
    

    Created on 2024-08-17 with reprex v2.1.0