For this assignment, I worked on importing data into RStudio and practicing basic R programming by testing and debugging a user-defined function. The goal was to understand how R functions handle variables and why correct naming is important. I started by creating a numeric vector in RStudio called assignment2, which contains twelve values. This vector was used as the input for testing the function. Next, I tested the provided myMean function by running myMean(assignment2) in the R console. Instead of returning a number, R produced an error.
The function failed because it referenced variables that did not exist. Although the function argument was named assignment2, the function body used the names assignment and someData. Since R functions can only access variables passed into the function or defined within it, this mismatch caused the error. To fix the problem, I updated the function so that it consistently used the argument name assignment2 when calculating the sum and length of the data. After correcting the variable names, the function successfully returned the mean.
assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
myMean <- function(assignment2) {return(sum(assignment2) / length(assignment2))}
myMean(assignment2)


Comments
Post a Comment