![]() |
Introduction
To R |
> x [1] 10The 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.414214Alternatively 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.
> y <- sqrt(5) a new variable called y > y+x add to x [1] 2.236068Notice 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...
> x <- 25 > x * sqrt(x) -> x1 > x2.1 <- sin(x1) > x2.2 _ cos(x1) > xsq <- x2.1**2 + x2.2**2And 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...