Module # 2 Assignment

> # Create the vector
> assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
> # Original (incorrect) function
> myMean <- function(assignment2) {
+   return(sum(assignment) / length(someData))
+ }
> # Test the original function (will give an error)
> myMean(assignment2)
Error in myMean(assignment2) : object 'assignment' not found
> # Corrected function
> myMean_corrected <- function(assignment2) {
+   return(sum(assignment2) / length(assignment2))
+ }
> # Test
> myMean_corrected(assignment2)
[1] 19.25

    The original function fails because it uses variable names that are not defined inside the function. The corrected version succeeds because it consistently uses the function argument assignment2 to calculate the mean.

Comments

Popular posts from this blog

Module # 4 Programming structure assignment

Assignment #10: Building Your Own R Package

Module # 8 Input/Output, string manipulation and plyr package