# The Assignment Function

When performing calculations, we may want to save the intermediate results for later use. This can be achieved by assigning values to symbolic variables using an "assign" function. Once you assign an object a designation, it stays in the working memory until you close the program. To see what objects are in the working memory, type ls(), or select Show Workspace command from the dropdown menu.

So to create a scalar constant x with value of 2, we type

> assign("x", 2)

This can also be simplified by using the operator <-

[Note that the symbol <- is made up from "less than" and "minus" with NO space between them.]

For example,

> x <- 2

> x

[1] 2

Assignment can also be done in the opposite direction using the symbol ->. For example,

> 3 -> y

The arrow for the assignment symbol always points to the name assigned to the vector.

We can also assign multiple objects the same value, as follows:

> x <- y <- 2

> x

[1] 2

> y

[1] 2

 Other commonly used operators are: 1.     arithmetic             + - * / ^ 2.     relational              > >= < <=                                     ==        (equals)                                     !=        (not equal) 3.     logical                   !           (NOT)                                     &         (AND)                                     |           (OR) 4.     assignment                        <- -> 5.     create a sequence  :

A complete listing can be found here: http://stuff.mit.edu/afs/sipb/project/r-project/arch/i386_rhel3/lib/R/library/graphics/html/plotmath.html.

## Vectors

Vectors are variables with one or more values of the same type, e.g., numerical, logical or character variables. For example, a numeric vector might consist of the numbers (1.2, 2.3, 0.2, 1.1). A vector can also have just a single variable.

## Concatenation

Vectors with multiple variables can be created using concatenation as indicated by the symbol c(). Thec stands for concatenation. The variables themselves are placed inside the rounded parentheses, i.e. ( ), not square [ ]  or curly { } brackets.

To create a vector named x, consisting of four numbers, naming 1.2, 2.3, 0.2 and 1.1, we can use the R command

> x <- c(1.2, 2.3, 0.2, 1.1)

> x

[1] 1.2 2.3 0.2 1.1

Also, we can use the function length() to find out how many elements the vector has.

> length(x)

[1] 4

If we want to select only some elements in the vector, then we can use indices. For example, if we want to know what the last three elements are in variable x, then we can type

> x[c(2,3,4)]

[1] 2.3 0.2 1.1

 What do you see in the screen if we type the following commands?   >x[-1] >  >x[2:4]   Based on your observation, what does the negative sign do within the index? Also, what does the function colon (:) mean? (This one will come in handy when we get to loops!)

You can clear the workspace by using the Edit dropdown menu and selecting "Clear console", or by typing:

> rm(list=ls())

### Logical Vectors and Logical Operators

R allows us to create logical vectors and to manipulate logical quantities as well. To create logical vectors, you may use TRUE, FALSE, or NA (for missing / not available) directly, or type in the condition/logic operation. Note that in order to be used in arithmetic calculations, R treats TRUE as 1 and FALSE as 0.

Let's look at some examples to see how these operators work.

> 1>=3

[1] FALSE

> !(1>3)

[1] TRUE

> (3 != 1) & (2 >= 1.9)

[1] TRUE

> y <- c(TRUE, FALSE, 5 > 2)

> y

[1]  TRUE FALSE  TRUE

> sum(y)

[1] 2

 If you type the following commands into R. What will (x, y, z, w) be?   > x <- !(5>=3) > y <- ((2^4) > (2*3)) > z<- x|y > w <- x&y