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

Vectors

The vector is R’s fundamental structure, and I showed it to you in earlier


examples.
It’s an array of data elements of the same type. The data elements in a vector are
called components. To create a vector, use the function c() , as I did in the
earlier
example:
> x <- c(3,4,5)
Here, of course, the components are numbers.
In a character vector, the components are quoted text strings (“Moe,” “Larry,”
“Curly”):
> stooges <- c("Moe","Larry", "Curly")
Strictly speaking, in the substr() example, “abcdefg” is a character vector with
one element.
It’s also possible to have a logical vector, whose elements are TRUE and FALSE , or
the abbreviations T and F :
> z <- c(T,F,T,F,T,T)
To refer to a specific component of a vector, follow the vector name with a brack-
eted number:
> stooges[2]
[1] "Larry"
Numerical vectors
In addition to c() , R provides seq() and rep() for shortcut numerical vector
creation.
Suppose you want to create a vector of numbers from 10 to 30 but you don’t feel
like typing all those numbers. Here’s how to do it:
> y <- seq(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

You might also like