# R

## Installing R and Tinn-R (an editor for R scripts)

## Most useful basic commands, summarized

Reference cards: R-Reference Card (The R-card).

By Tom Short (EPRI PEAC).

R-Reference Card

By Jonathan Baron (U Penn Psychology Department).

Basic 1 (less comprehensive than the R-card).

From William Revelle's R page (Northwestern Psychology Department).

Guides:

Quick-R.

R by example.

## Most useful commands, by topic

### Input/Output

R page on the subject- The easiest way to get data out of R is by using the
`write`

command.`write(data, "out.txt", ncolumns=2)`

produces a file out.txt to be produced, with all of the object "data" contained in two columns. The file will be produced here: "C:\Program Files\R\R-2.5.1" or the equivalent on your machine.

### Density estimation

R functions### Matrix and vector operations

Referencing entries in an array:To refer to the entry in the ith row of the jth column of matrix X, use:

`X[i,j]`

. To find
out the dimensions of an array X, use: `dim(X)`

. To find out the number
of rows, use: `nrow(X)`

. To
find the number of columns, use: `ncol(X)`

.Creating arrays:

R fills data columnwise into whatever shape you specify. Suppose you wanted to create a 3-by-2 matrix (3 rows and 2 columns). Then you would need to specify 6 entries to fill the matrix, and you would specify them in the order [1,1], [2,1], [3,1], [1,2], [2,2], [3,2]. The command to assemble a matrix is "

`matrix`

".
To build a 3-by-2 matrix X, use: ```
X
<-
matrix(c(2,3,-2,1,2,2),3,2)
```

. The same commands can be used to
create vectors (1-dimensional matrices). For example, to create a
row-vector with 3 entries, create a 1-by-3 matrix: `rv <- matrix(c(1,2,3),1,3)`

.
To create a column vector with the same entries as rv (the transpose of
rv, in fact), use: ```
rvt
<- matrix(c(1,2,3),3,1)
```

(or see the transpose operator explained below).Matrix multiplication:

Multiplying a scalar by a matrix can be done using the regular multiplication operator:

`*`

. Multiplying two matrices
together requires a special combination of characters. To multiply two
matrices X and Y, use: `X %*% Y`

.
A matrix Z can be created by multiplying X and Y: `Z <- X %*% Y`

.Matrix transpose:

To calculate the transpose of a matrix X, use: t(X). A matrix Xt can be created by transposing X:

`Xt <- t(X)`

.Matrix of ones, zeros, etc.:

You will often find it useful to create a matrix (or vector, etc.) of all ones (trust me, this is useful). To create a 3-by-2 matrix X of ones, use:

`X <- matrix(1,3,2)`

.
You can use the same command structure to create a 4-by-4 matrix Y of
all zeros: `Y <- matrix(0,4,4)`

.
In general, you can create an r-by-c matrix Z filled with identical
entries v using the command ```
Z
<- matrix(v,r,c)
```

.Identity matrix:

Use the

`diag`

command (to create a diagonal matrix) with the diagonal entries all
equal to 1. If you want to create a 3-by-3 identity matrix I, use: `I <- diag(c(1,1,1))`

. But what
if you wanted to make a 10-by-10 identity matrix? You wouldn't want to
type 10 ones inside the concatenation operator `c()`

. Instead, use the
repeat operator `rep()`

: `I <- diag(rep(1,10))`

. Inverse of a matrix:

Use the `solve`

command with only
one argument. To get the identity of a matrix X, use: `solve(X)`

.Concatenation of matrices:

To "paste" matrices together, use

`cbind`

and `rbind`

(column-bind and row-bind). If you use the operator `c()`

, you will lose the
dimensionality of the matrices. Try it.To see how

`cbind`

and `rbind`

work, try
pasting the following lines of code into R.

X <- matrix(c(1,2,3,4),2,2)

Y <- matrix(c(5,6,7,8),2,2)

Z <- cbind(X,Y)

Z

Z <- rbind(X,Y)

Z

More details are available in lots of places. Matrix Algebra in R by William Revelle of Northwestern U is pretty comprehensive [cached].

### Programming

Create a log file- sink

- Type
`sink("filename.txt"),`

then write your program, then`sink()`

again. There won't be any output to the console. Everything goes to filename.txt. Typing`sink()`

ends the connection and stuff will show up at the console again.