3.2 Descriptive statistics

Now let’s calculate some basic statistics on the entire dataset. We’ll calculate the mean age, maximum height, and number of pirates of each sex:

# What is the mean age?
mean(pirates$age)
## [1] 27

# What was the tallest pirate?
max(pirates$height)
## [1] 209

# How many pirates are there of each sex?
table(pirates$sex)
## 
## female   male  other 
##    464    490     46

Now, let’s calculate statistics for different groups of pirates. For example, the following code will use the aggregate() function to calculate the mean age of pirates, separately for each sex.

# Calculate the mean age, separately for each sex
aggregate(x = age ~ sex,
          data = pirates,
          FUN = mean)
##      sex age
## 1 female  30
## 2   male  25
## 3  other  27