0

I've written this piece of code and I fails saying this:

Error in if (biggest == Q) { : missing value where TRUE/FALSE needed

for (i in 1: 3) {
  if(i == 1) {
    t <- table(factor(d$s1q4, levels = 1:5),factor(d$s1q1, levels = 1:5))
    names(dimnames(t)) <- c(scenarioQuestions[4], scenarioQuestions[1])
  } else if (i == 2) {
    t <- table(factor(d$s1q6, levels = 1:5),factor(d$s1q2, levels = 1:5))
    names(dimnames(t)) <- c(scenarioQuestions[6], scenarioQuestions[2])
  } else {
    t <- table(factor(d$s1q3, levels = 1:5),factor(d$s1q5, levels = 1:5))
    names(dimnames(t)) <- c(scenarioQuestions[3], scenarioQuestions[5])
  }
  colnames(t) <- kano_categories
  rownames(t) <- kano_categories

  attributeName <- paste0("S", 1, ": ", attrNames[i])
  Q <- t[1][1] + t[5][5]
  M <- t[5][2] + t[5][3] + t[5][4]
  A <- t[1][2] + t[1][3] + t[1][4]
  I <- t[2][2] + t[2][3] + t[2][4] + t[3][2] + t[3][3] + t[3][4] + t[4][2] + t[4][3] + t[4][4]
  P <- t[1][5]
  R <- t[2][1] + t[3][1] + t[4][1] + t[5][1] + t[5][2] + t[5][3] + t[5][4]
 
  cats <- c(Q, M, A, I, P, R)
  biggest <- max(cats)
  biggest

  cat <- ""

  if(biggest == Q) {
    cat <- "Q"
  } else if (biggest == M) {
    cat <- "M"
  } else if (biggest == A) {
    cat <- "A"
  } else if (biggest == I) {
    cat <- "I"
  } else if (biggest == P) {
    cat <- "P"
  } else {
    cat <- "R"
  }

  df[nrow(df) + 1,] = c(attributeName, Q, M, A, I, P, R, cat)
}

The variables such as Q, M and P all get the NA value. Is this because I can't reference t outside of the if statement or what seems to be the problem here?

Data

table(factor(d$s1q4, levels = 1:5),factor(d$s1q1, levels = 1:5))
# gives   
#     1  2  3  4  5
#  1  0  0  0  0  0
#  2  0  0  1  4  0
#  3  0  4  1  1  0
#  4  4  8  1  1  0
#  5 16  4  0  2  1

If you look at this as a matrix, Q should be t[1][1] which is 0 + t[5][5] which is 1

I want to add these values for all variables such as Q, M, A, I, P and R

Mout Pessemier
  • 1,665
  • 3
  • 19
  • 33
  • 3
    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. We need to see what is actually stored in these variables to get some idea of what's going on. – MrFlick Apr 12 '22 at 16:27
  • 2
    i think by `t[5][2]` for example you mean `t[5, 2]`? `t[5]` selects one value from a 2-d table, there is not a second element to pick from `t[5]` – rawr Apr 12 '22 at 16:27
  • t[5][1] gives me 16 --> I view this table as a 2D matrix and I add the values I get into the variables such as Q, P, M, etc. – Mout Pessemier Apr 12 '22 at 16:29
  • `t[5][1]` may give you 16 but `t[5][2]` will be NA – rawr Apr 12 '22 at 16:29
  • Okay, I made the changes to t[5,2] and stuff, but now Q contains 1L. Why is there an L added at the end? – Mout Pessemier Apr 12 '22 at 16:38
  • @rawr Your solution works by putting them together as t[5,2] thanks! – Mout Pessemier Apr 12 '22 at 16:40

0 Answers0