Practice

From a package name to a line drawn through the points.

Twelve programs on packages that ship with R, on plots, and on a few summaries. Each one still has a check you answer here. Try it draws a figure when the program plots.

A library

library attaches a package

library puts a package on the search path. datasets ships with R, and mtcars is one of its tables. After library(datasets), the name mtcars can be used on its own.

library(datasets)
print(nrow(mtcars))

What to notice. nrow counts the rows. mtcars is a data frame of cars from Motor Trend, 1974. This page only uses packages that ship with R. It does not call install.packages.

How many rows does mtcars have?
What does library(datasets) do?
Try it

The package appears on the search path

search lists the places R looks for a name, in order. After library(datasets), one of those places is package:datasets.

library(datasets)
print("package:datasets" %in% search())

What to notice. %in% asks whether the string is one of the entries. TRUE means the package is attached.

What is printed?
Try it

:: reaches a package without attaching it

datasets::mtcars reads mtcars from that package even when you have not called library. The package name stays in the source, next to the object.

print(nrow(datasets::mtcars))

What to notice. Use library when you will use many names from one package. Use :: when you want one name and a visible package. Both find mtcars. Neither installs it.

What number is printed?
Try it

Graphs

plot draws a point for each pair

plot(span, knots) uses the first vector as the horizontal axis and the second as the vertical axis. Each position in the two vectors becomes one point.

span <- c(2, 4, 6)
knots <- c(3, 5, 7)
plot(span, knots)

What to notice. Try it draws the figure in this browser. The three points rise together: when span increases by 2, knots increases by 2.

How many points does this plot draw?
Which vector is the horizontal axis?
Try it

hist counts values into bins

hist groups numbers into bins and draws a bar for each count. The breaks here are 0, 4, and 8, so the bins are (0, 4] and (4, 8].

spans <- c(2, 2, 3, 5, 5, 5, 8)
counts <- hist(spans, breaks = c(0, 4, 8), plot = FALSE)$counts
hist(spans, breaks = c(0, 4, 8))
print(counts)

What to notice. plot = FALSE computes the counts without drawing. The second hist draws the same bins. 2, 2, and 3 fall in the first bin. 5, 5, 5, and 8 fall in the second.

What two counts are printed?
Try it

A box marks the median

boxplot draws a box from the lower quartile to the upper quartile. The line across the box is the median. The print shows that median as a number.

spans <- c(2, 4, 4, 6, 9)
boxplot(spans)
print(median(spans))

What to notice. Sorted, the values are 2, 4, 4, 6, 9. The middle one is 4, and that is the line in the box.

What number is printed?
Try it

abline adds the fitted line

plot opens the figure. abline(lm(knots ~ span)) draws the straight line from that fit on top of the points. These three points sit on the line knots = span + 1.

span <- c(2, 4, 6)
knots <- c(3, 5, 7)
plot(span, knots)
abline(lm(knots ~ span))

What to notice. abline needs a plot that is already open. The formula is the same shape as the model lesson: knots depends on span.

Where does the line sit relative to the points?
Try it

Summaries

Mean and median answer different questions

mean is the arithmetic average. median is the middle value after sorting. A large value pulls the mean and leaves the median where the middle is.

spans <- c(2, 4, 4, 6, 9)
print(c(mean(spans), median(spans)))

What to notice. The five numbers add to 25, so the mean is 5. Sorted, the middle number is 4, so the median is 4.

What two numbers are printed?
Try it

sd measures spread around the mean

sd is the standard deviation, the usual square-root measure of how far the numbers sit from their mean. round keeps two digits so the printed value is short.

spans <- c(2, 4, 4, 6, 9)
print(round(sd(spans), 2))

What to notice. The mean of these spans is 5. sd uses the sample denominator, n minus 1. A vector of identical numbers would have sd 0.

What number is printed?
Try it

table counts each category

table tallies how often each distinct value appears. The name in double brackets picks one of those tallies.

skies <- c("clear", "cloud", "clear", "clear")
print(table(skies)[["clear"]])

What to notice. cloud appears once. clear appears three times. A factor is not required. table works on a character vector.

What number is printed?
Try it

cor is 1 when the points are a straight rise

cor(span, knots) is the Pearson correlation. It is 1 when one vector is a straight increasing line of the other, and -1 when that line falls.

span <- c(2, 4, 6)
knots <- c(3, 5, 7)
print(cor(span, knots))

What to notice. knots is span plus 1. Adding a constant and keeping a positive slope does not change this correlation. It is exactly 1.

What number is printed?
Try it

A paired test uses the differences

t.test(..., paired = TRUE) compares two measurements on the same units. The estimate is the mean of the differences, after minus before.

before <- c(3, 4, 5, 6)
after <- c(4, 6, 6, 9)
test <- t.test(after, before, paired = TRUE)
print(unname(test$estimate))

What to notice. The four differences are 1, 2, 1, and 3. Their mean is 1.75. The test also reports a p-value. This lesson prints only the mean difference.

What number is printed?
What does paired = TRUE change?
Try it