Module # 2 Assignment

I have downloaded and reviewed the import instructions. 


Evaluate myMean Function: 

Initial 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)

Output: 
> myMean(assignment2)
Error in myMean(assignment2) : object 'assignment' not found

Corrected Code: 
assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
myMean <- function(assignment2) {
  return(sum(assignment2) / length(assignment2))
}

myMean(assignment2)

Output: 
> myMean(assignment2)
[1] 19.25

Explanation: 
The provided myMean function generated an error message indicating that an object was not found when I tested it in RStudio using the vector "assignment2". This happened because the code tried to use variable names (assignment and "someData") that weren't supplied as arguments or defined within the function. Because R functions only accept variables that are either generated inside the function body or passed in as parameters, the function failed. Despite being successfully given into the function, "assignment2" was never utilized in the computation. Rather, the problem was caused by the function referring to nonexistent variables.


Comments

Popular posts from this blog

Assignment #1

Module # 6 Doing math in R part 2

Module # 3 data.frame