how do we calculate mean,median,mode etc.
let me start by saying to calculate them is just as easy as typing 3+3 to add two number on your calculator. the logic is that we make use of some inbuilt function in R. these are the inbuilt function and what we use them for.
mean() :- to calculate the mean of the data.
median() :- to calculate the median of the data
sort() :- to arrange the data in ascending order.
var() :- to calculate the varience
sd() :- to calculate the standard deviation
range() :- to calculate the range
sum():- to calculate the sum
min() :- to get the minimum value from the list or vector.
max() :- to get the maximum value from the list or vector.
Now let's try to make use each one of them
let's say we have the following data as the score of 20 student in STA114 test below.
22,22,12,25,23,21,11,13,12,25,26,21,12,11,22,11,16,17,18,28,29,10,20 and 8.
the question is to calculate
i. the mean
ii. the median
iv. standard deviation
we need to write all the data as vector then proceeding to solve all the question by assigning the formula to suitable variable and print the answer with the following codes
allScore=c(22,22,12,25,23,21,11,13,12,25,26,21,12,11,22,11,16,17,18,28,29,10,20,8) meanScore=mean(allScore) print(meanScore) medianScore=median(allScore) print(medianScore) S.D=sd(allScore) print(S.D) variance=var(allScore) print(variance) range=range(allScore)
Score=c(22,22,12,25,23,21,11,13,12,25,26,21,12,11,22,11,16,17,18,28,29,10,20,8) > meanScore=mean(allScore) > print(meanScore)  18.125 > medianScore=median(allScore) > print(medianScore)  19 > S.D=sd(allScore) > print(S.D)  6.347286 > variance=var(allScore) > print(allScore)  22 22 12 25 23 21 11 13 12 25 26 21 12 11 22 11 16 17 18 28 29 10 20 8
Now to find the range we know that range is the largest data minus the smallest data. so to know the largest and the smallest we need to arrange the data in ascending order then pick the last and first (biggest and smallest) and subtract.
look at the code below
arrangedScore=sort(allScore) biggest=arrangedScore(arrangedScore) smallestScore=arrangedScore() print(biggest) print(smallestScore)