Calculating the mean of a Dataset in R



Welcome!


In the following Blog, we will demonstrate how to calculate the mean (average) of a dataset in R using a simple custom function. We define a function called myMean that takes a numeric vector as input and returns the mean of its values. Given the dataset:

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

The function computes the mean to be 18.67. This illustrates how easily statistical calculations can be performed in R with a few lines of code.

Here’s what assignment2 does:

  • It creates a numeric vector that contains a sequence of numbers: 6, 18, 14, 22, 27, 17, 22, 20, 22.
  • This vector is a collection of data points that might represent a series of observations or value
assignment2 serves as the input data for the function myMean.The function takes this vector as an argument and then processes it to calculate the mean.The variable assignment2 acts as a container that organizes the data into a structured format — a numeric vector. This organization is necessary for functions in R to perform calculations, such as summing up numbers or determining their count. Since the myMean function relies on assignment2 to compute the mean, assignment2 can be considered an essential "functional component" in the process. It determines the outcome of the function by providing specific data points that the function uses.

Following is the code used in R studio! Feel free to try this out in your R environment to get hands-on practice with writing and executing custom functions!

INPUT:

OUTPUT:





Comments