top of page

R Programming Journal Module 2 - Wesley Huang

  • Writer: Wesley Huang
    Wesley Huang
  • Feb 2
  • 1 min read
Screenshot showing the error produced by the original myMean function in RStudio.
Screenshot showing the error produced by the original myMean function in RStudio.

Why the Function failed


The function failed because the variable names used inside the function do not match the function’s input argument. The function is defined to take in a parameter called assignment2, but inside the function body it attempts to use variables named assignment and someData, which were never defined. Since R requires variable names to match exactly, it cannot find these objects and returns an “object not found” error instead of calculating the mean.


Screenshot showing the corrected version returning the mean of the vector.
Screenshot showing the corrected version returning the mean of the vector.

Why the Function succeeded


The function succeeded after the variable names inside the function were corrected to match the input argument. In the revised version, the function consistently uses a single parameter, x, to calculate both the sum and the length of the data. Because the same variable name is used throughout the function, R is able to correctly evaluate the expression and return the mean of assignment2, which is 19.25.

 
 
 

Comments


bottom of page