Chapter2 - Chapter 2 R Chapter 2 Simple Manipulations -...

Chapter 2 R - 1 -Chapter 2 Simple Manipulations --- Numbers and Vectors 2.1 Vectors and Assignment R operates on named data structures. The simplest such structure is the numeric vector , which is a single entity consisting of an ordered collection of numbers. To set up a vector named x , say, consisting of five numbers, namely 10.4, 5.6, 3.1, 6.4 and 21.7, use the R command > x <- c(10.4, 5.6, 3.1, 6.4, 21.7) #set up a vector > x [1] 10.4 5.6 3.1 6.4 21.7 > x[2] This is an assignment statement using the function c() which in this context can take an arbitrary number of vector arguments and whose value is a vector got by concatenating its arguments end to end. Notice a few things a. We assigned the values to a variable called x b. The assignment operator is a <- , which consists of the two characters ‘<’ (“less than”) and ‘-’ (“minus”) occurring strictly side-by-side and it ‘points’ to the object receiving the value of the expression. . In most contexts the ‘ = ’ operator can be used as an alternative. Both will be used, although, you should learn one and stick with it. c. The value of the x doesn't automatically print out. It does when we type just the name though as the last input line indicates. d. The value of x is prefaced with [1]. This indicates that the value is a vector. More on that later.

This preview
has intentionally blurred sections.
Sign up to view the full version.

Chapter 2 R - 2 -e. A number occurring by itself in an expression is taken as a vector of length one. f. Assignment can also be made using the function assign() . An equivalent way of making the same assignment as above is with: > assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7)) The usual operator, <-, can be thought of as a syntactic short-cut to this. g. Assignments can also be made in the other direction, using the obvious change “->” in the assignment operator. So the same assignment could be made using > c(10.4, 5.6, 3.1, 6.4, 21.7) -> x If an expression is used as a complete command, the value is printed and lost. So now if we were to use the command > 1/x #reciprocal of x [1] 0.09615385 0.17857143 0.32258065 0.15625000 0.04608295 the reciprocals of the five values would be printed at the terminal (and the value of x, of course, unchanged), but 1/x will be lost in R. More Example : The following assignment > y <- c(x, 0, x) would create a vector y with 11 entries consisting of two copies of x with a zero in the middle place.

Chapter 2 R - 3 -2.2 Vector Arithmetic 1. Vectors can be used in arithmetic expressions , in which case the operations are performed element by element . > x <- c(1, 2, 3) > y <- c(4, 5, 6) > v <- 2*x + y + 1 Note: Vectors occurring in the same expression need not all be of the same length. If they are not, the value of the expression is a vector with the same length as the longest vector which occurs in the expression. Shorter vectors in the expression are recycled as often as need be (perhaps fractionally) until they match the length of the longest vector. In particular a constant is simply repeated. > x1 <- c(1,2)

This preview
has intentionally blurred sections.
Sign up to view the full version.