Memra

R descriptives and summary()

◈ 7 cards

mean, sd, var, quantile and summary() — reading the printed output, and knowing exactly which cells will not match the hand rule.

The output you will be handed

The paper prints R output and asks you to read it. For one variable, four calls cover it: mean(), sd() (and var()), quantile() and summary(). Two of them agree with your hand arithmetic to the last digit; two use a convention the course does not, and the paper expects you to know which.

Worked example — the audit hours

> hrs <- c(42, 38, 51, 45, 39, 49)
> mean(hrs); sd(hrs); var(hrs)
[1] 44
[1] 5.291503
[1] 28

Exactly the L3.3 values. var() divides by — the 28, not 23.3 — and sd() is its square root printed to R's default seven significant digits. A ; separates statements on one line, and each prints on its own line.

Worked example — the eleven overdue invoices

> od <- c(1, 2, 4, 5, 7, 8, 10, 12, 15, 19, 41)
> summary(od)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    4.50    8.00   11.27   13.50   41.00 

Set the printed cells beside L3.4's hand values:

CellHand (median-of-halves)R (summary)Match?
Min11.00yes
1st Qu.44.50no
Median88.00yes
Mean11.2711.27yes
3rd Qu.1513.50no
Max4141.00yes

R's quartiles come from its type-7 rule: it places at sorted position and interpolates halfway between the 3rd value (4) and the 4th (5), giving 4.5; at position 8.5, halfway between 12 and 15, giving 13.5. Median-of-halves takes the median of 1, 2, 4, 5, 7 and of 10, 12, 15, 19, 41. Both are legitimate definitions of a quartile; they only agree by coincidence. quantile(od, c(0.25, 0.75)) prints the same two numbers with percentage labels:

> quantile(od, c(0.25, 0.75))
 25%  75% 
 4.5 13.5 

Which cells are convention-free

Min, median, mean and max have one definition, so they always match a correct hand answer. The two quartiles depend on the rule, so when a question prints summary() and asks for the IQR, use the printed quartiles (13.50 − 4.50 = 9); when it gives raw data and says "compute", use median-of-halves (15 − 4 = 11). Never mix the two in one answer.

Reading habits carry over from L2.7: [1] is a line-position marker, not a value; summary() rounds to four significant digits (11.27, not 11.27273); R's sd() and var() are sample versions and never need an correction.

NORMAL ~/memra/learn/afm-113/r-descriptives-and-summary utf-8 LF