Vectors

I think of a vector as an ordered column of data. It can be consist of numbers or text, but its basic construction is as an ordered column of data. The following sequences are all examples of vectors.

1:15 #generates a sequence from 1 to 15 by 1;
> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

seq(from = 1, to = 25, by = 1) #generates a sequence from 1 to 25 by 1;
> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

seq(1, 25, 1) #generates a sequence from 1 to 25 by 1;
> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Again, we can assign names to these types of objects. Below, the name X is assigned to a vector of integers from 1 to 15 and Y is assigned to a vector with three elements: 1,2,3.

X <- 1:15
Y <- 1:3
X
> [1] 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
Y
> [1] 1 2 3

The important thing is that vectors need not be sequences. Let’s assign some values to Z, using the concatenate function which takes n elements and makes them a vector using the following command: c(element 1, element 2, … , element n ).

Z <- c(143, 5640, 2601, 902, 506) 
Z
> [1] 143 5640 2601 902 506

Now the vector can be operated upon using typical mathematical functions, which are conducted element-wise meaning each element within the vector has the functino performed on it in order. Below we define a vector Z and then add ten to each element.

Z <- c(143, 5640, 2601, 902, 506) 
Z
> [1] 143 5640 2601 902 506

Z + 10
> [1] 153 5650 2611 912 516

Alternately we could square each element:

Z <- c(143, 5640, 2601, 902, 506) 
Z^2
> [1]  20449 31809600  6765201   813604   256036

However, vectors of numbers are not the only kind of vectors we can make. For example, we can make a vector of text by putting each element inside quotation marks, but we would not be ablet o perform any mathematical operations on that vector.

People <- c("Professor X.", "Professor Y.", "Professor Z.")
People
People + 6
> ## Error in People + 6: non-numeric argument to binary operator
> [1] "Professor X." "Professor Y." "Professor Z."

Objects and Data Structures

Objects and Assigning Values


What makes R a more flexible programming language than, say, Stata, is that it allows us to call objects by ‘name.’ What the heck does that mean? It means that as an object-oriented programming language, R allows us to store objects on the workspace and give them a name. Then, instead of retyping or re-entering whatever data we have stored, we can simply refer to the object we have created and named. We do this using an assignment arrow which is a ‘less than’ symbol followed by a dash, with the name of the object on the left side and whatever is being assigned to it on the right hand side. For example, X <- 2, produces an object ‘X’ (note that R is case sensitive) and assigns to it the numeric value of two. Below, X is a type object called a ‘scalar,’ and its class (as indicated using the class command) is numeric.

X <- 2
X
> [1] 2

sqrt(X)
> [1] 1.414214

X^2
>[1] 4

x # here I typed a lower case x, which returns the error below. 
> ## Error in eval(expr, envir, enclos): object 'x' not found

class(X)
> [1] "numeric"

This might not seem that important to you now, but having the ability to name objects and call them by name is essential when one wants to write a program to solve a particular problem. Given that the size of R’s workspace is only limited by the memory of the computer one is using, it is easier to name more objects if doing so makes your code clearer. The best code is that which is clear not only to the original programmer, but would also be clear to anyone who begins from the top of the script and executes the code line-by-line. Comments can help with clarity, as can clear object names.