Introduction to Vectors

(Section 2.1)

What is a Vector?

Vectors

In R, a vector is a sequence of values. The values are called the elements of the vector.

Example:

3, 7, 10.1, -5

Another example:

“Ginger”, “Tina”, “Sita”, “Raj”

(They are not the same thing as vectors in math or physics.)

Two Types of Vectors in R

  • atomic (all the elements are of the same data type, or just “type”)
  • non-atomic (the elements can be any R-objects at all)

We won’t learn about non-atomic vectors until Chapter 9.

What is a “Type”?

Vector Type

Any one of the six basic forms the elements in an atomic vector can take. The four types we will encounter the most are:

  • double
  • integer
  • character
  • logical

Example of a Vector

numVec <- c(23.2, 45, 631, -273, 0, 48.371, 100000,
            85, 92, -236, 8546, 98774, 0, 0, 1, 3)
numVec
 [1]     23.200     45.000    631.000   -273.000      0.000     48.371
 [7] 100000.000     85.000     92.000   -236.000   8546.000  98774.000
[13]      0.000      0.000      1.000      3.000

About the c() Funcion

The c() function “combines” values to make a vector.

Finding the Type of a Vector

typeof(numVec)
[1] "double"

double is short for “double-precision.” It refers to a way that real numbers are stored in computers.

Integer Vectors

intVec <- c(3L, 17L, -22L, 45L)
intVec
[1]   3  17 -22  45
typeof(intVec)
[1] "integer"
  • The L specifies that the value must be stored as a whole number.
  • We don’t use integer vectors very often.

Character Vectors

These are made up of strings.

strVec <- c("Brains", "are", "not", "the", "best", 
            "things", "in", "the", "world", "93.2")
strVec
 [1] "Brains" "are"    "not"    "the"    "best"   "things" "in"     "the"   
 [9] "world"  "93.2"  
typeof(strVec)
[1] "character"

Logical Vectors

logVec <- c(TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE)
logVec
[1]  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE

In order to represent a logical value you use:

  • TRUE to represent truth;
  • FALSE to represent falsity.

Practice

What would happen if you entered this command?

anotherLogical <- c(TRUE, false)

Why?

Coercion

What Would Happen …

… if you tried to represent falsity with the string "false"?

newVector <- c(TRUE, "false")

Not a Logical Vector!

newVector
[1] "TRUE"  "false"

newVector is not a logical vector. Check it out:

typeof(newVector)
[1] "character"

Coercion

  • All of the elements of an atomic vector have to be of the same type
  • so when c() has to combine vectors of different types
  • R coerces some of the elements to other types …
  • … until all are of the same type

Coercion Rules

The rules for coercion are a bit complicated, but you’ll eventually know that:

  • character beats double,
  • which in turn beats integer,
  • which in turn beats logical.

Examples

typeof(c("one", 1, 1L, TRUE))
[1] "character"
typeof(c(1, 1L, TRUE))
[1] "double"
typeof(c(1L, TRUE))
[1] "integer"

Coercion with the as.-Functions

numVec <- c(3, 2.5, -7.32, 0)
as.character(numVec)
[1] "3"     "2.5"   "-7.32" "0"    
as.integer(numVec)
[1]  3  2 -7  0
as.logical(numVec)
[1]  TRUE  TRUE  TRUE FALSE

Of course there is also an as.numeric() function!

Combining Vectors

Combination Upon Combination!

numVec1 <- c(5, 3, 10)
numVec2 <- c(1, 2, 3, 4, 5, 6)
numCombined <- c(numVec1, numVec2)
numCombined
[1]  5  3 10  1  2  3  4  5  6

NA Values

Heights of Some People

heights <- c(72, 70, 69, 58, NA, 45)
heights
[1] 72 70 69 58 NA 45

The NA means that one of the heights was not recorded, or went missing somehow.

R Can Give NA

If R cannot do a requested operation, it might return NA:

as.numeric(c("5", "-3.27", "four"))
Warning: NAs introduced by coercion
[1]  5.00 -3.27    NA

R could figure out how to convert the first two strings to numbers, but did not know what to do with "four".

Practice

How many NAs do you think this vector will have?

as.numeric(c("-7", "3.235", "zero", "5e07"))

Try it! (You might be surprised.)

“Everything is a Vector”

There Are No “Single Values” in R

17
[1] 17

It’s really a numerical vector, of length 1.

NA is a Vector

Even NA is just a vector of length 1

NA
[1] NA

It’s a logical vector:

typeof(NA)
[1] "logical"

Yep, Just a Vector!

Even the type of a vector is a vector!

typeof(heights)
[1] "double"

It’s a character vector, of length 1.

Named Vectors

Naming the Elements of a Vector

The elements of a vector can have names, if we like:

ages <- c(Bettina = 32, Chris = 64, Ramesh = 101)
ages
Bettina   Chris  Ramesh 
     32      64     101 

Two Ways to Name the Elements

Name at the time of creation:

ages <- c(Bettina = 32, Chris = 64, Ramesh = 101)

Name after creation:

names(heights) <- c("Scarecrow", "Tinman", "Lion", 
                    "Dorothy", "Toto", "Boq")
heights
Scarecrow    Tinman      Lion   Dorothy      Toto       Boq 
       72        70        69        58        NA        45 

Special Character Vectors

R comes with two handy, predefined character vectors:

letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"

We will make use of them from time to time.

Length of a Vector

The length() Function

length(heights)
[1] 6

It tells you how many elements the vector has.

Practice

What is the length of the vector LETTERS?

Write an R command to get the length of LETTERS.