Descriptives (means, sd, etc)

Packages used: DescTools, Base R, Base Stats

Method One: Basic functions

General coding:

Let’s first create a numeric vector:

x <- c(1.4, 5.66, 7.13, 9.21)

The max() and min() functions return the maximum and minimum of a vector:

max(x)

[1] 9.21

min(x)

[1] 1.4

The mean() and median() functions return the mean and median:

mean(x)

[1] 5.85

median(x)

[1] 6.395

The mode() comes from the DescTools package

mode(x)

Worked out example:

### Read the data
# Read a csv file, named “demo01.csv”

demo01<-read.csv(“demo01.csv”)

#DescTools can give us a number of descriptive statistics. Mode doesn’t make a
#lot of sense for continuous variables, but I’m doing it here just to show we can
# If you’ve not previously installed the DescTools package,
# uncomment and run the next line first.
# mean, min and max are base R functions, and don’t come from DescTools

#install.packages(‘DescTools’)
library(DescTools)

mean(demo01$weight)
SD(demo01$weight)
min(demo01$weight)
max(demo01$weight)
Median(demo01$weight)
Mode(demo01$weight)