Vectors in R

A vector is a one-dimensional array of values.

If we have a single value, R sees this value as a vector of length 1:

x <- 7

print (length(x))
## [1] 1

The length of an object is provided by the function length().

The c() (or combine) function allows you to create your own vectors:

c(1, 2, 3, 4)
## [1] 1 2 3 4

Let’s assign that function call to an object:

y<- c(1, 2, 3, 4)

print(y)
## [1] 1 2 3 4

What is the length of y?

length(y)
## [1] 4

y is an example of a vector. Its length is 4 because it contains four elements.

Applying functions to vectors

We can apply the functions that we have used before to vector objects as well.

vec1 <- c(1, 2, 3, 4)
sqrt(vec1)
## [1] 1.000000 1.414214 1.732051 2.000000

sqrt(vec) returns the square root of each element in that vector

abs(vec1)
## [1] 1 2 3 4
vec2 <- c(-1, -2, -3, -4)
abs(vec2)
## [1] 1 2 3 4
vec3 <- c(1:10)
mean(vec3)
## [1] 5.5

What did the function mean() do?

The functions we applied to the vector objects vec1, vec2, and vec3 are applied to each element of the vector objects.

Subsetting: Taking individual elements from vectors

We use square brackets to pull out specific elements from a vector:

vec3 <- c(1:10)

vec3[5]
## [1] 5

We can use the c() function to get a few elements at a time:

vec3[c(2,4,6)]
## [1] 2 4 6

And we can operate on these objects, even if we don’t name them!

length(vec3[c(2,4,6)])
## [1] 3

Wow. Things are getting a bit complex!

Let’s break that down:

The object vec3 was defined in line 88 as a vector of length ten, containing the values 1 through 10.

We pulled out the second, fourth, and sixth elements of vec3 by subsetting.

Then, we found the length of our subset of vec3.

R is very powerful, and we will use these ideas extensively in this course.

Types in R

What is the difference between these the two R objects x and y?

x <- c(2:8, 23, 111)

print(x)
## [1]   2   3   4   5   6   7   8  23 111
y <- c(2:8, "Peterkins", TRUE, 128.74)

print(y)
##  [1] "2"         "3"         "4"         "5"         "6"         "7"        
##  [7] "8"         "Peterkins" "TRUE"      "128.74"

x contains only numbers whereas y contains multiple data types:

What does R think of x and y?

typeof(x)
## [1] "double"
typeof(y)
## [1] "character"

“double” is R’s way of saying that the elements of x are decimal numbers. “character” means that R is treating the elements of y as text.

typeof(8)
## [1] "double"
typeof("Peterkins")
## [1] "character"
typeof(TRUE)
## [1] "logical"
typeof(128.74)
## [1] "double"

Ok, so even though the elements of y are of different types, when we string them together, R treats them all as characters.

This is called coercion. The element types are put on an equal footing, using the most permissive type present.

Can we do operations in R with x and y?

x/2
## [1]  1.0  1.5  2.0  2.5  3.0  3.5  4.0 11.5 55.5
y/2

There is a construct in R that allows us to preserve the nature or intent of our elements. It’s called a list.

z <- list(2:8, "Peterkins", TRUE, 128.74)

print(z)
## [[1]]
## [1] 2 3 4 5 6 7 8
## 
## [[2]]
## [1] "Peterkins"
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] 128.74
z[[1]]/2
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0
z[[2]]/2
z[[4]]/2
## [1] 64.37

Congratulations!

You assimilated a lot of new R concepts! 1. We dramatically expanded our repertoire of functions. 2. We created and operated with arrays. 3. We learned how R represents and deals with different data types.

We are ready to move onto matrices!!