Module # 2: RStudio and myMean

 

The Function myMean

1) This week we read the first three chapter of  our text book "The Art of R programming" by Matloff, and Chapter of our textbook called R packages by Wickham. The first three chapter of Matloff go over the basic set of R, how vectors, arrays, matrices, scalars, rows, columns, R testing, packages and other basic aspects of coding work inside of R. I have taken a few classes with R in the past and there is not much new here. It was a good refresher though for this assignment.  

2) The assignment I was tasked for this week was to evaluate the following function call myMean, provided with the data for myMean; assignement2. This is how it looked like: 
> assignment2<-c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
> myMean<-  function(assignment2) { return(sum(assignment)/length(someData)) }
> myMean(assignment2)
 
3) The problem with the code above is that a lot of it is undefined. There is no data provided called "assignment" or called "someData". When run as-is in Rstudio, we get "Error in myMean(assignment2) : object 'assignment' not found", because it is not defined. If we wanted to fix it then to create something out of it we could do the following. Replace "assignment" with "assginement2", and "someData" with a "assignment2". Doing this we get the following:
> assignment2<-c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
> myMean<-  function(assignment2) { return(sum(assignment2)/length(assignment2)) }
> 
> 
> myMean(assignment2)
[1] 19.25
>
4) As we can see above, the mean for "assignement2" is 19.25. There is no error being thrown out now, since all the variables of the function myMean are now defined and refer to the same set of numbers.  

Comments

Popular posts from this blog

Module # 10 Building my own R package

Module # 13 Shiny Web App

Module # 4 Programming Structure