Memra

?mean, defaults, and the first R gotcha

◈ 12 cards

round(511.6667, digits = 2) and round(digits = 2, x = 511.6667) both print 511.67 — named arguments go in any order. ?mean shows na.rm = FALSE, so mean(c(420, NA, 760)) is NA; add na.rm = TRUE and it is 590. install.packages() once; library() every session.

Arguments by position, or by name

A spreadsheet function takes its arguments in a fixed order: =ROUND(511.6667, 2). R functions take them the same way — or by name, in any order:

> round(511.6667, digits = 2)
[1] 511.67
> round(digits = 2, x = 511.6667)
[1] 511.67

x and digits are the argument names, and once named they can come in any order. Unnamed arguments are matched by position, so round(511.6667, 2) also works. Naming is the habit to build: aggregate(amount ~ prov, data = orders, FUN = sum) reads as a sentence because its arguments are named.

Worked example — reading a help page

How do you know the names? Type ?mean in the console and the help page opens in the bottom-right pane. Its Usage line is the function's signature:

mean(x, ...)
## Default S3 method:
mean(x, trim = 0, na.rm = FALSE, ...)

Read it as: x is required; trim and na.rm have defaults, shown after the =, and are used unless you say otherwise. The ... passes anything else through. args(round) prints the same information in the console: function (x, digits = 0, ...) — so round(511.6667) with no digits gives 512.

The first gotcha: na.rm = FALSE

That default is the one every R newcomer meets first. Take three amounts with one missing:

> x <- c(420, NA, 760)
> mean(x)
[1] NA

Not an error, not a warning — NA. R's reasoning is that the mean of three values, one unknown, is unknown. The spreadsheet's AVERAGE would have skipped the blank and printed 590 without a word, and Module 2 called that the silent path. R makes you say what to do:

> mean(x, na.rm = TRUE)
[1] 590
> sum(is.na(x))
[1] 1

na.rm = TRUEremove the NAs — averages the two known values. is.na(x) is the logical vector FALSE TRUE FALSE, and sum() of it counts the missing values, which is COUNTBLANK. Every summary function — sum, mean, sd, min, max, median — has the same argument with the same default; a summary that prints NA is telling you to look for a blank, not that the code is wrong. Treating the NA as zero would give 393.33, which is a different — and wrong — question.

Packages: install once, load every session

Base R has mean(), aggregate(), barplot(). Everything else comes in packages. Two verbs, and they are not interchangeable:

  • install.packages("readr") downloads the package to this machine. Once.
  • library(readr) loads it into this session. Every time R starts.

The two failures read differently. Load a package that was never installed:

> library(readr)
Error in library(readr) : there is no package called ‘readr’

Call a function from a package that is installed but not loaded, and the error names the function, not the package:

> str_trim("a")
Error in str_trim("a") : could not find function "str_trim"

Could not find function means one of two things: a typo in the name, or the package that owns it has not been library()-ed this session. This course is base R; tidyverse names appear only so you can recognise them.

Type three lines, then rescue a mean

Type the named round, the na.rm mean and the NA count. The numeric asks what the rescued mean prints.

CRISP-DM: na.rm is a data preparation → clean data decision made inside a data understanding → describe data call.

NORMAL ~/memra/learn/afm-112/functions-named-arguments-and-na-rm utf-8 LF