Reputation: 11
I only get this error in attempting to knit in R Markdown to a Word document. The code runs fine elsewhere (in the Rscript).Also I have already had previous code showing:
numericScores = transform(correctedScores, beer2_score = as.numeric(beer2_score))
numericScores = transform(correctedScores, beer3_score = as.numeric(beer3_score))
numericScores = transform(correctedScores, beer4_score = as.numeric(beer4_score))
numericScores = transform(correctedScores, beer5_score = as.numeric(beer5_score))
numericScores = transform(correctedScores, beer6_score = as.numeric(beer6_score))
# testing to see if they are indeed numeric
sapply(numericScores, mode)
correctedGuesses = Guesses[complete.cases(Guesses), ]
str(correctedGuesses)
correctedGuesses
correctedScores = numericScores[complete.cases(numericScores), ]
str(correctedScores)
correctedScores
########
# trying to put scores in correct order
# first I will label the beers with their names
for(i in 1:45){
for(j in 2:7) {
if (Order[i,j] == 1) { Order[i, j] = "Miller"}
if (Order[i,j] == 2) { Order[i, j] = "Natural"}
if (Order[i,j] == 3) { Order[i, j] = "Keystone"}
if (Order[i,j] == 4) { Order[i, j] = "Busch"}
if (Order[i,j] == 5) { Order[i, j] = "Bud"}
if (Order[i,j] == 6) { Order[i, j] = "Miller"}
}
}
# Deleting the unused/unavailable Order rows
Order = Order[-c(4, 33),]
Miller_sc = 0
Natural_sc = 0
Keystone_sc = 0
Busch_sc = 0
Bud_sc = 0
B = c(
Miller_sc,
Natural_sc,
Keystone_sc,
Busch_sc,
Bud_sc )
for (i in 1:43) {
for (j in 2:7) {
if (Order[i,j] == "Miller") {B[1] = B[1] + correctedScores[i,j]}
if (Order[i,j] == "Natural") {B[2] = B[2] + correctedScores[i,j]}
if (Order[i,j] == "Keystone") {B[3] = B[3] + correctedScores[i,j]}
if (Order[i,j] == "Busch") {B[4] = B[4] + correctedScores[i,j]}
if (Order[i,j] == "Bud") {B[5] = B[5] + correctedScores[i,j]}
}
}
The error reads:
Error in B[3] + correctedScores[i, j]: non-numeric argument to binary operator Calls: ... handle ->withCallingHandlers -> withVisible -> eval -> eval Execution halted
Upvotes: 1
Views: 1589
Reputation: 45027
The usual reason for an error like this is that the code in your document refers to a variable in your global environment. When you knit the document, it can't see that variable, so you get an error.
Most commonly that error would be some variation on "variable not found". You're getting a different error, so R is finding a variable with the right name somewhere else.
Without the full document, we can't tell you for sure what variable would be causing your problems. When I take a quick look at your script, I see these variables with no definitions:
correctedScores
beer2_score # etc., though these might be columns in correctedScores
Guesses
Order
I also notice that the first 4 lines of your script do nothing, since the 5th line overwrites their result.
Upvotes: 1