The provided dataset
assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
The provided code
myMean <- function(assignment2) { return( sum(assignment) / length(someData) )}
myMean <- function(assignment2) {
# commented out due to error:
# return(sum(assignment)/length(someData))
# corrected version:
return(sum(assignment2)/length(assignment2))
}
#tested <- myMean(assignment2)
Discussion
The code as provided will ultimately cause two errors:
- Error in myMean(assignment2) : object ‘assignment’ not found
- Error in myMean(assignment2) : object ‘someData’ not found
The code is intended to calculate the average of the dataset through a user-defined function. The two errors indicate that the function is attempting to access two unknown or undeclared variables: assignment
and someData
. Both unknowns should be replaced with the function’s receiving argument: assignment2
.
So, corrected…the function now reads:
myMean <- function(assignment2) { return( sum(assignment2) / length(assignment2) )}
The call myMean(assignment2)
will return myMean
as: 19.25.
Sanity check, the built-in mean {base}
function call of mean(assignment2)
returns: 19.25.
Reference:
- Matloff, The Art of R Programming, section 1.3
GitHub
Related file(s) can be found at Git Me
No comments:
Post a Comment