R notes

From raju

R Configuration

show help in a browser

To show help in a browser

options(help_type="html")

to revert it back and show help in the R console

options(help_type="text")

Add this to ~/.Rprofile to set the option every time R is started.

For a list of possible help_type values, see ?help and search for help_type. Currently, for R 3.1.1-1, they are "text", "html" and "pdf". Case is ignored and partial matching is allowed.

Ref:-

Debian R

R reference

  • www.stat.berkeley.edu/~s133/all2011.pdf - self contained, easy to understand.

octave and R

See also: http://cran.r-project.org/doc/contrib/R-and-octave.txt

Item octave R notes
clear all the variables clear rm(list=ls())
vector of zeros zeros(1,5) numeric(5)
logical vector filled with FALSE's false(1,5) vector(length=5)
logical vector filled with TRUE's true(1,5) !vector(length=5)
matrix of ones ones(2,3) array(1, c(2,3))

R tasks

Reading tab delimited data

Consider the tab delimited data file

=> cat data.txt
date    Steve   Bill    Larry
10/31/2014      1.0     1.2     1.4
10/30/2014      -2.0    -2.2    2.3
10/29/2014      0.3     2.3     0.5
10/28/2014      1.2     0.9     -1.9
10/27/2014      0.1     0.6     0.5
10/24/2014      7.0     3.3     2.7
10/23/2014      1.2     6.8     2.1
10/22/2014      1.9     7.1     4.8
10/21/2014      0.7     0.4     1.4

Use the R code below to read it

=> cat read_data.R
setwd("~/work/r_programming/read_data/")
xx<-read.delim("data.txt", header=T)
xx$date<-as.Date(as.character(xx$date), "%m/%d/%Y")

sample run

=> R
> source('read_data.R')
> xx
        date Steve Bill Larry
1 2014-10-31   1.0  1.2   1.4
2 2014-10-30  -2.0 -2.2   2.3
3 2014-10-29   0.3  2.3   0.5
4 2014-10-28   1.2  0.9  -1.9
5 2014-10-27   0.1  0.6   0.5
6 2014-10-24   7.0  3.3   2.7
7 2014-10-23   1.2  6.8   2.1
8 2014-10-22   1.9  7.1   4.8
9 2014-10-21   0.7  0.4   1.4

The read.delim is a wrapper around read.table with modified defaults. We can also use read.table to do this.

=> cat read_table.R
setwd("~/work/r_programming/read_data/")
xx<-read.table("data.txt", header=T, sep="\t")
xx$date<-as.Date(as.character(xx$date), "%m/%d/%Y")

sample run

> source('read_table.R')
> xx
        date Steve Bill Larry
1 2014-10-31   1.0  1.2   1.4
2 2014-10-30  -2.0 -2.2   2.3
3 2014-10-29   0.3  2.3   0.5
4 2014-10-28   1.2  0.9  -1.9
5 2014-10-27   0.1  0.6   0.5
6 2014-10-24   7.0  3.3   2.7
7 2014-10-23   1.2  6.8   2.1
8 2014-10-22   1.9  7.1   4.8
9 2014-10-21   0.7  0.4   1.4
> dim(xx)
[1] 9 4

Understanding the out of bounds error

Create an empty list

> A <- list()
> A
list()

Add a vector of two elements as the first component

> A[[1]] <- c(2,6)
> A
[[1]]
[1] 2 6

Add another vector of three elements as the third component

> A[[3]] <- c(1,3,2)
> A
[[1]]
[1] 2 6

[[2]]
NULL

[[3]]
[1] 1 3 2

Notice how the second component is automatically created and set to NULL. We can apply is.null() on the second component.

> is.null(A[[2]])
[1] TRUE

However, applying is.null() on fourth component will result in an error since the list only has three components.

> is.null(A[[4]])
Error in A[[4]] : subscript out of bounds
> length(A)
[1] 3

Calling a Linux only program from Windows in R

Here is one way to call a Linux only program from Windows. It uses putty's plink.exe to connect and run the command.

> a <- system('c:/path/to/putty/plink.exe -ssh user@hostname -pw xxxxxxxx "command to be run on Linux machine"', intern=T)
> a
  [1] "line1"
  [2] "line2"
  [3] "line3"
...

The variable a contains whatever is displayed on the terminal. Assuming that the Linux command generates an output file, out.txt, and also that we can samba mount the linux drive onto windows as D:, the output file can also be read directly.

> b <- readLines("D:/out.txt")
> b
  [1] "line1"
  [2] "line2"
  [3] "line3"
...

The above approach requires inputting passwords explicitly on the command line. A more secure way is to use key-based authentication to login.

> a <- system('c:/path/to/putty/plink.exe -ssh user@hostname -i "c:/path/to/key/file" "command to be run on Linux machine"', intern=T)

Instructions to set up the keys file can be found in http://kb.norsetech.net/use-putty-to-automatically-login-a-ssh-session/

Finally, the above Linux command runs in the home directory of the Linux machine by default. To run it in a different directory, use

> a <- system('c:/path/to/putty/plink.exe -ssh user@hostname -i "c:/path/to/key/file" "cd ~/change/directory; command to be run on Linux machine"', intern=T)

Miscellaneous

Just for fun

> require(fortunes)
> fortune("brain surgery")

Initialize a data frame to zeros

a <- data.frame( matrix(0, nrow=5, ncol=3))
names(a) <- c("myCol1", "myCol2", "myCol3");