Module # 2 Assignment: Debugging an R Function: myMean()
Testing the myMean Function in Rš
I tested this function in RStudio:
myMean <- function(assignment2) {
return(sum(assignment) / length(someData))
}
I used this vector:
assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
And I ran this entire code:
assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
myMean <- function(assignment2) {
return(sum(assignment) / length(someData))
}
myMean(assignment2)
I received the following error message:
Error in myMean(assignment2) : object 'assignment' not found
Explanation
The function failed because the variable names inside the function do not match the function argument. The function argument is named assignment2, but the code refers to assignment and someData, which do not exist. Hence why I got an error. And the corrected way to fix it is below.
input:
assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
myMean <- function(assignment2) {
return(sum(assignment2) / length(assignment2))
}
myMean(assignment2)
[1] 19.25
Picture:
Comments
Post a Comment