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