DEV Community is a community of 636,824 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

How do we write a program to solve for mean?

firstly keep calm as it is a little thing. Please if you are new to R-programming then I recon mended this link
for you to start from the begining.Before we could write any code we need to understand the nature of the problem itself.let's start from the terms central tendency.

What Is the central tendency?

The central tendency is a single number that represents the most common value for a list of numbers.
There are many ways to calculate the central tendency for a data sample, such as the mean which is calculated from the values, the mode, which is the most common value in the data distribution, or the median, which is the middle value if all values in the data sample were ordered.

what is average?

The average is the common term for the mean. They can be used interchangeably.
The mean is different from the median and the mode in that it is a measure of the central tendency that is calculated from the data. As such, there are different ways to calculate the mean based on the type of data.
Three common types of mean calculations that you may encounter are the arithmetic mean, the geometric mean, and the harmonic mean which are going to deal with in this lesson.
Let’s take a closer look at each calculation of the mean in turn.

Arithmetic Mean

The arithmetic mean is calculated as the sum of the values divided by the total number of values, referred to as N.
• Arithmetic Mean = (x1 + x2 + … + xN) / N
A more convenient way to calculate the arithmetic mean is to calculate the sum of the values and to multiply it by the reciprocal of the number of values (1 over N); for example:
• Arithmetic Mean = (1/N) * (x1 + x2 + … + xN)
where x1,x2,x3...xN represent the value of the data and N is the number of data.
The arithmetic mean is appropriate when all values in the data sample have the same units of measure, e.g. all numbers are heights, or dollars, or miles, etc.
I believe you have little mathematics knowlege on how to use the formula. Now let's take a look at example.

Example1

lets say the following data show the salary of 10 workers and we need to find the arithmetic mean.
10 000,12 000,11 000,11 500, 9 000, 12 500,10 500, 11 000, 13 000 and 12 500.
so our code will look like the following;

``````allSalaries= c(10000,12000,11000,11500,9000,12500,10500,11000,13000,12500)
A_M=mean(allSalaries)
print(A_M)
``````

Result>

``````> allSalaries = c(10000,12000,11000,11500,9000,12500,10500,11000,13000,12500)
> A_M=mean(allSalaries)
> print(A_M)
[1] 11300
``````

so 11300 is the mean as easy as that using the mean() function. you might also want to learn about median,mode,variance,standard deviation and more on this Arithmetic Mean please click on this link
Now let's proceed to the other mean.

How to calculate Geometric Mean.

so what is geometric Mean?
The geometric mean is calculated as the N-th root of the product of all values, where N is the number of values.

Geometric Mean = N-root(x1 * x2 * … * xN)
For example, if the data contains only two values, the square root of the product of the two values is the geometric mean. For three values, the cube-root is used, and so on.
The geometric mean is appropriate when the data contains values with different units of measure, e.g. some measure are height, some are dollars, some are miles, etc.

Example2

let's say we need to calculate the geometric mean for example 1 data

solution

we know that Mean = N-root(x1 * x2 * … * xN)
G.M= (x1 * x2 * … * xN)^1/N
therefore
N=10
Mean=(10000*12000*11000*11500*9000*12500*10500*11000*13000,12500)^1/10
GM=11235.33
Now let's see the way to write code for it.

How to use R-programming to calculate Geometric Mean.

to use R-programming to calculate Geometric Mean copy the following codes and study them.
Code

``````allSalaries= c(10000,12000,11000,11500,9000,12500,10500,11000,13000,12500)
productOfAll=prod(allSalaries)
N=length(allSalaries) #this is to get the number of data
GM=productOfAll^(1/N)
print(GM)
``````

Result>>

``````>allSalaries= c(10000,12000,11000,11500,9000,12500,10500,11000,13000,12500)
>productOfAll=prod(allSalaries)
> N=length(allSalaries) #this is to get the number of data
> GM=productOfAll^(1/N)
> print(GM)
[1] 11235.33
``````

study the code well, there is no big deal in it, it is just that we make use of prod() and length() to find the product of all items and the number of items respectively.
However if i did not mention this it will hurt as you might wish to learn to the last gram. you see the same formula we use above had be reconstructed to
HM=(x1 * x2 * … * xN)^(1/N)(taking the log of bothside)
log(GM)=log(x1 * x2 * … * xN)^(1/N)
log(GM)=(1/N)log(x1 * x2 * … * xN)
log(GM)=(1/N)(logx1 + logx2 +logx3 … logxN)(multiply change to plus in rule of logarithm.)
therefore GM=antilog of {(1/N)(logx1 + logx2 +logx3 … logxN)}
now we can say GM is the antilog of the sum of the logarithm of the sample divided by number of samples.
let's see another way to solve the last example using this formula.
Solution2
we need the logarithm of all the items then the sum of all
we also need inverse of N and antilog(10^).
so our code look like the following.
codes

``````allSalaries= c(10000,12000,11000,11500,9000,12500,10500,11000,13000,12500)
logOfAll=log10(allSalaries)
N=length(allSalaries) #this is to get the number of data
sumOfLog=sum(logOfAll)
GMlog=sumOfLog^(1/N) #this is the logarithm of GM
GM=10^GMlog
print(GM)
``````

Result>>

``````> allSalaries= c(10000,12000,11000,11500,9000,12500,10500,11000,13000,12500)
> logOfAll=log10(allSalaries)
> N=length(allSalaries) #this is to get the number of data
> sumOfLog=sum(logOfAll)
> GMlog=sumOfLog/N) #this is the logarithm of GM
> GM=10^GMlog
> print(GM)
[1] 11235.33
``````

Amazing! we get the same answer!
now that we have seen how to easily calculate the geometric Mean let's proceed to Harmonic Mean.

How to calculate Harmonic Mean

The harmonic mean is calculated as the number of values N divided by the sum of the reciprocal of the values (1 over each value).
• Harmonic Mean = N / (1/x1 + 1/x2 + … + 1/xN)
If there are just two values (x1 and x2), a simplified calculation of the harmonic mean can be calculated as:
• Harmonic Mean = (2 * x1 * x2) / (x1 + x2)
The harmonic mean is the appropriate mean if the data is comprised of rates.
Recall that a rate is the ratio between two quantities with different measures, e.g. speed, acceleration, frequency, etc.
Example3
assuming the following data show the average speed of a motors. calculate the mean.
30,40,60,45,35,40,45,40
solution.
N=8
HM=N/ (1/x1 + 1/x2 + … + 1/xN)
HM=8/ (1/30+1/40+1/60+1/45+1/35+1/40+1/45+1/40)
HM=40.4008

How to use R-programming to calculate the Harmonic Mean

by definition it is clear that we will need the inverse of all the data, then the sum of all the inverse and the number of sample.
here our code below
Codes

``````allSpeed= c(30,40,60,45,35,40,45,40)
N=length(allSpeed)
inverseOfAllSpeed=allSpeed^(-1)
sumOfInverse=sum(inverseOfAllSpeed)
HM=N/sumOfInverse
print(HM)
``````

Result>>

``````> allSpeed= c(30,40,60,45,35,40,45,40)
> N=length(allSpeed)
> inverseOfAllSpeed=allSpeed^(-1)
> sumOfInverse=sum(inverseOfAllSpeed)
> HM=N/sumOfInverse
> print(HM)
[1] 40.4008
``````

woow! we did it, incredible solution. I hope you enjoy this lesson?. you can also read.

5🔥 Ploting graph with R

If you have any question concerning this lesson you can chat me up on whats-app(07045225718) or drop your comment here. have a nice day!