Introduction to Basic R functions and Data Structures



Introduction to Basic R functions and Data Structures 



Firstly, let's understand the function:


myMean <- function(assignment2) { 

  return(sum(assignment2)/length(assignment2)) 

}

This function, myMean, takes a vector assignment2 as an argument. It calculates the mean (average) of the values in the vector using the formula: sum of values divided by the number of values.


Now, let's evaluate the function with the provided data:


assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)

result <- myMean(assignment2)

print(result)

This will output the mean of the assignment2 vector.


Explanation:


sum(assignment2) calculates the sum of all values in the vector.

length(assignment2) gives the number of elements in the vector.

The division of the sum by the length gives the mean.

Comments