Vectors
The function c(), which stands for concatenate, is useful for creating vectors.
Another useful function for creating vectors is the seq() function, which generates sequences.
Subsetting lets us access specific parts of a vector by using square brackets to access elements of a vector.
codes <- c(380, 124, 818)
country <- c("italy", "canada", "egypt")
# We can also name the elements of a numeric vector
# Note that the two lines of code below have the same result
codes <- c(italy = 380, canada = 124, egypt = 818)
codes <- c("italy" = 380, "canada" = 124, "egypt" = 818)
# We can also name the elements of a numeric vector using the names() function
codes <- c(380, 124, 818)
country <- c("italy","canada","egypt")
names(codes) <- country
# Using square brackets is useful for subsetting to access specific elements of a vector
codes[2]
codes[c(1,3)]
codes[1:2]
# If the entries of a vector are named, they may be accessed by referring to their name
codes["canada"]
codes[c("egypt","italy")]
Vector Coercion
In general, coercion is an attempt by R to be flexible with data types by guessing what was meant when an entry does not match the expected. For example, when defining x as
R coerced the data into characters. It guessed that because you put a character string in the vector, you meant the 1 and 3 to actually be character strings, "1" and "3".
The function as.character() turns numbers into characters. The function as.numeric() turns characters into numbers. In R, missing data is assigned the value NA.
Sorting
The function sort() sorts a vector in increasing order.
The function order() produces the indices needed to obtain the sorted vector, e.g. a result of 2 3 1 5 4 means the sorted vector will be produced by listing the 2nd, 3rd, 1st, 5th, and then 4th item of the original vector.
The function rank() gives us the ranks of the items in the original vector.
The function max() returns the largest value, while which.max() returns the index of the largest value. The functions min() and which.min() work similarly for minimum values.
data(murders)
sort(murders$total) #$
x <- c(31, 4, 15, 92, 65)
x
sort(x) # puts elements in order
index <- order(x) # returns index that will put x in order
x[index] # rearranging by this index puts elements in order
order(x)
murders$state[1:10] #$
murders$abb[1:10] #$
index <- order(murders$total) #$
murders$abb[index] #$ order abbreviations by total murders
max(murders$total) #$ highest number of total murders
i_max <- which.max(murders$total) #$ index with highest number of murders
murders$state[i_max] #$ state name with highest number of total murders
x <- c(31, 4, 15, 92, 65)
x
rank(x) # returns ranks (smallest to largest)
Vector Arithmetic
In R, arithmetic operations on vectors occur element-wise. For a quick example, suppose we have height in inches:
and want to convert to centimeters. Notice what happens when we multiply inches by 2.54:
[1] 175 157 168 178 178 185 170 185 170 178
In the line above, we multiplied each element by 2.54.
If we have two vectors of the same length, and we sum them in R, they will be added entry by entry as follows:
\[\begin{pmatrix}a\\ b\\ c\\ d \end{pmatrix} + \begin{pmatrix}e\\ f\\ g\\ h \end{pmatrix} = \begin{pmatrix}a+e\\ b+f\\ c+g\\ d+h \end{pmatrix} \]