Functions

Hello world! As part of my final adventures as an undergraduate student at the University of South Florida, I am taking an Advanced Statistics course under Dr. Alon Friedman. As part of our course requirements, we are to maintain a blog consisting of all assignments completed throughout the class. This is such a blog! I hope you enjoy following along in my statistics journey, and let’s get started!

To start off the course, this week’s assignment was to evaluate the following function “myMean”:

> assignment2 <- c(6,18,14,22,27,17,22,20,22)

> myMean <- function(assignment2) {return(sum(assignment2)/length(assignment2))} 

The first line of code here creates a vector named assignment2 with the elements 6, 18, 14, 22, 27, 17, 22, 20, and 22. The second line of code creates a function named myMean. This function takes an input (in this case, our vector assignment2) and returns the sum of the input divided by the length of the input. When myMean is ran with assignment2 as the input, assignment2’s mean 18.66667 is returned!

R output:

> assignment2<- c(6,18,14,22,27,17,22,20,22)
> myMean <- function(assignment2) 
+           {
+             return(sum(assignment2)/length(assignment2))
+           } 
> 
> myMean(assignment2)
[1] 18.66667

Pretty simple stuff, but a solid foundation in R is critical to proceeding into more advanced analysis. See you next week!

Leave a comment