Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

On my screen, and probably on yours too, all the elements in y appear on one line.

The printed page, however, is not as wide as the Console pane. Accordingly,
I separated the output into two lines. I do that throughout the book, where
necessary.
R has a special syntax for a numerical vector whose elements increase by 1:
> y <- 10:30
> y
[1] 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
[18] 27 28 29 30
If you want the elements to increase in steps of 2, use seq like this:
> w <- seq(10,30,2)
> w
[1] 10 12 14 16 18 20 22 24 26 28 30
You might want to create a vector of repeating values. If so, rep() is the function
to use:
> trifecta <- c(6,8,2)
> repeated_trifecta <- rep(trifecta,4)
> repeated_trifecta
[1] 6 8 2 6 8 2 6 8 2 6 8 2
Another way to use rep() is to supply a vector as the second argument. Remember
from the earlier example that x is the vector (3,4,5) What happens if you supply
x as the second argument for rep() ?
> repeated_trifecta <- rep(trifecta,x)
> repeated_trifecta
[1] 6 6 6 8 8 8 8 2 2 2 2 2
The first element repeats three times; the second element, four times; and the
third element, five times.
Matrices
A matrix is a 2-dimensional array of data elements of the same type. In statistics,
matrices are useful as tables that hold data. (Advanced statistics has other appli-
cations for matrices, but that’s beyond the scope of this book.)

You might also like