14  Functions

R comes with a large library of built-in functions. A handful cover most of the day-to-day work of summarizing a dataset — the rest of this book adds many more, but these are worth knowing cold.

14.1 sum() and length()

sum() totals a set of numbers; length() counts how many values are in a vector.

14.2 mean() and summary()

mean() returns the average of a numeric vector; summary() returns a quick five-number-plus-mean overview (min, 1st quartile, median, mean, 3rd quartile, max) — often the fastest way to sanity-check a new dataset.

14.3 sqrt()

sqrt() computes a square root — for example, standard deviation is often built from a sum of squared deviations, so sqrt() shows up constantly once you reach descriptive statistics (Topic 7).

14.4 data.frame(), head(), and View()

data.frame() builds a table-like structure with named columns of possibly different types — the everyday format for a dataset. head() previews the first few rows without printing the whole thing; View() opens the data in a spreadsheet-style tab within RStudio.

View(field_data) would open this table in its own tab — this only works in a full RStudio session, not in the browser-based examples in this book.

Summary

Concept Description
Key Built-in Functions
sum() and length() Total a vector's values, and count how many values it has.
mean() and summary() The average of a vector, and a quick min/quartile/median/mean/max overview.
sqrt() The square root — recurs throughout descriptive statistics.
data.frame(), head(), and View() Build a table of named columns; preview the first rows; open it in a spreadsheet-style viewer.