2.2 Vector operations

A lot of data manipulation in R is based on logical checks like the ones shown above. We can take these one step further to actually perform what one might think of as a “query” to select certain elements of a vector that satisfy some condition.

For example, we can reference specific elements of vectors directly. Here, we specify that we want to print the third element of a.

# This one just prints it
a[3]
## [1] 3

We might want to store that value to a new object f that is easier to read and type out.

# This one stores it in a new object
# f is way easier to type than a
f <- a[3]


Important

If it is not yet obvious, we have to assign the output of functions to new objects for the values to be usable in the future. In the example above, a is never actually changed. This is a common source of confusion early on.

Going further, we could select vector elements based on some condition. On the first line of code below, we tell R to show us the indices of the elements in vector b that match the character string c. Out loud, we would say, “b where the value of b is equal to c” in the first example. We can also use built-in R functions to just store the indices for all elements of b where b is equal to the character string "c".

b[b == "c"]
## [1] c
## Levels: a b c d e
which(b == "c")
## [1] 3

Perhaps more practically speaking, we can do elementwise operations on vectors easily in R. Here are a bunch of different things that you might be interested in doing with the objects that we’ve created so far. Give a few of these a try.

a * .5 # Multiplication
a + 100 # Addition
a - 3 # Subtraction
a / 2 # Division
a^2 # Exponentiation
exp(a) # Same as "e to the a"
log(a) # Natural logarithm
log10(a) # Log base 10

If we change b to character, we can do string manipulation, too!

# Convert b to character
b <- as.character(b)

We can append text. Remember, the examples below will just print the result. We would have to overwrite b or save it to a new object if we wanted to be able to use the result somewhere else later.

# Paste an arbitrary string on to b
paste(b, "AAAA", sep = "")
## [1] "aAAAA" "bAAAA" "cAAAA" "dAAAA" "eAAAA"
# We can do it the other way
paste("AAAA", b, sep = "")
## [1] "AAAAa" "AAAAb" "AAAAc" "AAAAd" "AAAAe"
# Add symbols to separate
paste("AAAA", b, sep = "--")
## [1] "AAAA--a" "AAAA--b" "AAAA--c" "AAAA--d"
## [5] "AAAA--e"
# We can replace text
gsub(pattern = "c", replacement = "AAAA", b)
## [1] "a"    "b"    "AAAA" "d"    "e"
# Make a new object
e <- paste("AAAA", b, sep = "")

# Print to console
e
## [1] "AAAAa" "AAAAb" "AAAAc" "AAAAd" "AAAAe"
# We can strip text
# (or dates, or times, etc.)
substr(e, start = 5, stop = 5)
## [1] "a" "b" "c" "d" "e"

We can check how many elements are in a vector.

# A has a length of 5,
# try it and check it
length(a)
## [1] 5
# Yup, looks about right
a
## [1] 1 2 3 4 5

And we can do lots of other nifty things. We can also bind multiple vectors together into a rectangular matrix. Say what?