Descriptive Statistics of numeric variables using R

Data
score = c(12, 14, 17, 13, 19, 28, 20, 9, 3, 6, 5, 11, 12, 17, 16, 8, 6, 2)

1. Mean
round(mean(score), 1)

2. Median
median(score)

3. Mode (https://stat.ethz.ch/pipermail/r-help/2011-March/273569.html)
smode=function(x){
    xtab=table(x)
    modes=xtab[max(xtab)==xtab]
    mag=as.numeric(modes[1]) #in case mult. modes, this is safer
    themodes=names(modes)
    mout=list(themodes=themodes,modeval=mag)
    return(mout)}

4. Range
therange =  max(score) - min(score)
print(therange)

5. Interquartile range
mystats = summary(score)
IR = mystats[5] - mystats[2]
print(IR)

6. Variance

6.1 Sample variance
var(score)

6.2 Population variance
sum((score-mean(score))^2)/length(score)
 
7. Standard variation

7.1 Sample StdVar
sd (score)

7.2 Population StdVar
sqrt(sum((score-mean(score))^2)/length(score))

8. Coefficient of variation (CV)
sd(score)/abs(mean(score))

9. Skewness
library(moments)
skewness(score)

10. Kurtosis
library(moments)
kurtosis (score)

The above examples are contained in the paragraph 2.2 of the book "Στατιστική ανάλυση με τη γλώσσα R" (in Greek, ISBN: 978-960-93-9445-1) published in Thessaloniki, 2017.

Comments