Contents
 
Introduction
To R

Storing Values in R Objects

R is an object-oriented language: variables, data, matrices, functions, etc are stored in the active memory of the computer in the form of objects. For example, if an object x has for value 10:
> x                 
[1] 10
The digit 1 within brackets indicates that the display starts at the first element of x.

You can store a value in a named `object' by assigning it with the <- (or ->) symbol. For example:

> x <- sqrt(2)      set x to the square root of two
> x                 just type x to print its value
[1] 1.414214
Alternatively you can use the reverse symbol -> or the underscore _ character. The three lines below are identical:
> x <- sin(pi)
> sin(pi) -> x
> x_sin(pi)
In these notes preference is given to the first of these methods.
Assignment is usually pronounced "gets" by R users. Read x <- sqrt(2) as "x gets square root of 2".
You can then do arithmetic with x as you might expect.
> y <- sqrt(5)      a new variable called y
> y+x               add to x
[1]  2.236068
Notice that if you type an assignment to R, it doesn't print anything, but if you just type an expression the result is printed.

Variable names must start with a letter, and may contain letters, numbers and dots. Upper and lower case are assumed different.

Try and use names that mean something. Having twenty objects called a1 through to a20 will be confusing...

Examples

Here are some valid examples:
> x <- 25
> x * sqrt(x) -> x1
> x2.1 <- sin(x1)
> x2.2 _ cos(x1)
> xsq <- x2.1**2 + x2.2**2
And some invalid ones...
> a = 10         R does not permit `=' for assigning values
> 99a <- 10      `99a' doesn't start with a letter
> a1 <- sqrt 10  No parentheses to sqrt
> a1_1 <- 10     Can't use underscore in a name
> a-1 <- 99      Or hyphens...
> sqrt(x) <- 10  Silly...

Contents