In statistics, mean is defined as the ‘average’ value that you get when you add all the values and divide them by the total number of values.
This tutorial is focused on finding the mean of the values using three different methods.
Table of Contents
Find the mean of an input list of values
In this method, we are creating a list including some values and try to find the mean of those values.
x <- c(21,32,43,54,65,76,87,98) #calculates the mean for the given values mean(x) Output --> 59.5
In the above code, we first created a list ‘x’ containing some values. Then, with the help of function mean(), we can find the mean of the values in the list ‘x’.
x <- c(2.5,3.7,6.8,5.7,6.8,4.6) #calculates the mean for the decimal values mean(x) --> 5.016667
Find the mean of the values in a text file
In this method, you will get to know how you can find the mean of the values which are stored in a text file.
x <- read.table('testvalues.txt') #This will read the text file
mean(x$V1) #This will calculate the mean of the values in the text file --> 75.33333
Find the mean of the values stored in a CSV file
In this method, we are importing a CSV file, which includes some values in it. Then, as you did in the above steps, you can find the mean of the values using function mean().
The below code demonstrated the importing and reading of a CSV file.
x <- read.csv('testdata1.csv')
The above image shows the values which are present in the CSV file. In the next step, we can calculate the mean of these values. For that purpose run the below code.
mean(x$Values) #calculates the mean for the values present in CSV file --> 68.92857
R is a great statistical analysis language. It offers a range of functions for mathematical computations. In this tutorial, we have used mean() function to find the mean of given values. You can find the mean of values by creating a list of values, a text file or a CSV file as shown above as well.
I hope you got some good understanding of finding the mean of values using different methods. That’s all for now, happy learning!!!