Aggregate 1

This example provides the function with a vector of numeric values, penguins$flipper_length_mm and a factor, penguins$species.

require(palmerpenguins)

aggregate(
  x = penguins$flipper_length_mm, 
  by = list(penguins$species), 
  FUN = mean, na.rm = TRUE)
NA

Aggregate Method 2

In this example, Iā€™m using the formula notation:


aggregate(
  formula = flipper_length_mm ~ species, 
  data = penguins,
  FUN = mean, na.rm = TRUE)
NA

Boxplot with formula notation

The formula notation is handy for making conditional boxplots

boxplot(
  formula = flipper_length_mm ~ species, 
  data = penguins)

Aggregating by more than one factor

Here is how you can aggregate by species and sex:

aggregate(
  formula =  flipper_length_mm ~ species + sex, 
  data = penguins,
  FUN = mean, na.rm = TRUE)

Doubly conditional boxplot

The x-axis labels need help, but the box-and-whisker plots are now aggregated by species and sex.

boxplot(
  formula = flipper_length_mm ~ species + sex, 
  data = penguins)

Calculate critical t-values

n = nrow(penguins)

alpha = 0.05

qt(1 - alpha / 2, n - 1)
[1] 1.966904
qt(alpha/2, n - 1)
[1] -1.966904
LS0tDQp0aXRsZTogIkFnZ3JlZ2F0ZSBEZW1vIg0Kb3V0cHV0OiANCiAgaHRtbF9ub3RlYm9vazoNCiAgICB0b2M6IFRSVUUNCiAgICB0b2NfZmxvYXQ6IFRSVUUNCmVkaXRvcl9vcHRpb25zOiANCiAgY2h1bmtfb3V0cHV0X3R5cGU6IGlubGluZQ0KLS0tDQoNCmBgYHtyIHNldHVwLCBpbmNsdWRlPUZBTFNFfQ0Ka25pdHI6Om9wdHNfY2h1bmskc2V0KGVjaG8gPSBUUlVFKQ0Ka25pdHI6Om9wdHNfY2h1bmskc2V0KHdhcm4gPSBGQUxTRSkNCnJlcXVpcmUocGFsbWVycGVuZ3VpbnMpDQpgYGANCg0KIyBBZ2dyZWdhdGUgMQ0KDQpUaGlzIGV4YW1wbGUgcHJvdmlkZXMgdGhlIGZ1bmN0aW9uIHdpdGggYSB2ZWN0b3Igb2YgbnVtZXJpYyB2YWx1ZXMsIGBwZW5ndWlucyRmbGlwcGVyX2x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