Notes for R  ---- By Li Chen


Topics


Useful websites

Back to top


R and Splus

Back to top


R package

Back to top


R plot

Back to top


Write R program

Back to top


Use R packages

library( ) --- to find out which additional packages are available on your system.
       
library(geoR) --- to load the installed package geoR.

library(help=geoR) --- to find out which functions provide by the package geoR.
help(package=geoR) ---same as library(help=geoR).

detach("package:geoR") --- to unload the loaded package geoR.

library(akima) --- the package for interpolation of irregularly spaced data.
 
Back to top



Enable Profiling of R's Execution

Rprof(filename = "Rprof.out", append = FALSE, interval = 0.02)
filename is used for recording the profiling results. Set to 'NULL' or ' " " ' to disable profiling. The 'summaryRprof' function can be used to process the output file to produce a summary of the usage.

For example:

f<-function(x,a) sum((x-a)^2)
Rprof("out")
nlm(f, c(10,10), a=c(3,5))
Rprof(" ")
summaryRprof("out")

Back to top



Get system time

system.time(expr)
Return CPU times that "expr" used.A numeric vector of length 5 containing the user cpu, system cpu, elapsed, subproc1, subproc2 times. The subproc times are the user and system cpu time used by child processes (and so are usually zero).

For example:

system.time(for(i in 1:50) x<-mean(rnorm(1000)))

[1]  0.28 0.00 0.28 0.00 0.00

The other way to get system time is using proc.time() function.

For example:

now<-proc.time()
for(i in 1:50) x<-mean(rnorm(1000))
proc.time()-now

[1] 0.29 0.01 0.31 0.00 0.00

system.time is preferred.

Back to top



Write efficient code

For example to calculate the mean of 1, 2, ..., 10000.
x<-c(1:10000)

sum<-0
now<-proc.time()
for(i in 1:10000)
     sum<-sum+x[i]
meanx<-sum/10000
proc.time()-now
#[1] 0.41 0.00 0.42 0.00 0.00

use vectorized arithmetic instead of loops

system.time(sum(x)/length(x))
#[1] 0.01 0.00 0.01 0.00 0.00

use R function.
system.time(mean(x))
#[1] 0 0 0 0 0

Back to top


Draw a map

library(fields)
US( )

library(maps) is another useful package for creating maps.

Back to top



Set postscript diagrams

ps.options( )-- check the current setting of postscript file.

postscript("plot.ps", width=6, append=TRUE) --The plot will be landscape orirntation.
#postscript("plot.ps", horizontal=FALSE)
###plots
dev.off( )

Back to top



Plot in color

The color of postscript file is same as the color showed in R window . (cool!)

A  description of how colors are specified is given below.
     `col' A specification for the default plotting color. 
     `col.axis' The color to be used for axis annotation.
     `col.lab' The color to be used for x and y labels.
     `col.main' The color to be used for plot main titles.
     `col.sub' The color to be used for plot sub-titles.

Color Specification:

Colors can be specified in several different ways. The simplest way is with a character string giving the color name (e.g.,  `"red"').  A list of the possible colors can be obtained with the function `colors'. Alternatively, colors can be specified directly in terms of there RGB components with a string of the form  `"#RRGGBB"' where each of the pairs `RR', `GG', `BB' consist of  two hexadecimal digits giving a value in the range `00' to `FF'.  Colors can also be specified by giving an index into a small table  of colors, the `palette'.  This provides compatibility with S.  Index `0' corresponds to the background color.  Additionally, `"transparent"' or (integer) `NA' is transparent,  useful for filled areas (such as the background!), and just  invisible for things like lines or text.  The functions `rgb', `hsv', `gray' and `rainbow' provide  additional ways of generating colors.


Back to top


Write R output as a file

sink ( ... ) --- send R output to a file
cat ( ... ) --- print the arguments, coercing them if necessary to character mode first.

Back to top


Convert a Splus object to R object

For example, the data object "test" will be converted from a Splus object to R object.

In Splus:
  test<-c(rep(1,1000)) # test is a vector
  dump("test")             # test.q will be generated under the current working directory.

Open R under the current working directory
  source("test.q")
  test      # This would be exactly same as "test" in Splus

You can also try to program a little bit to convert all the Splus objects to R objects. For example, in Splus,
  aa<-objects( )  #get all objects
  for ( a in aa )    
   {  cat (a, " ")
       dump(a)
    }
Then, in R, just read in whatever you want by using "source" command.

Back to top


Plot legend  outside current  plot region
 par(mfrow=c(1,2),xpd=NA)
 plot(1:5)
 legend(locator(1),pch=1,legend="test") #click  the mouse on the device

Back to top


Plot two variables with different scales in one plot
x<-rnorm(10)                                                # generate x from N(0,1)    
y<-rnorm(10,mean=100,sd=10)                   #generate y from N(100,100)
ry<-range(x,y/100)                                       # range is obtained by rescale y
plot(c(1,10),ry,type="n",axes=F,xlab="n",ylab=" ", main="x and y")
axis(2,col=1)                                                 # define axis on the left for x
mtext("x",side=2, line=2)
axis(1,1:10,as.character(1:10))                     #axis on the bottom
lines(1:10,x,lty=1,lwd=2,col=1)                
ats<-axTicks(2)                                            # get the tickmark locations for the left axis
axis(4,ats,as.character(100*ats),col=2)       # define axis on the right
mtext("y",side=4,line=2,col="red")
lines(1:10,y/100,lty=1,lwd=2,col=2)
legend(1,ry[2],legend=c("x","y"),col=c(1,2),lty=c(1,1),lwd=c(2,2))

Back to top


Create a plot panel
split.screen(rbind(c(0,0.4,0,1),c(0.4,1,0,1))) # divide the screen into two part, left and right. The left one is small and the right one is large.
left<-split.screen(c(2,1),screen=1)   # further divide the left one into two: upper and  lower
screen(left[1])                                      # plot on the upper left part
#par(mar=c(0.05,0.05,0.05,0.05))    # set margin
plot(1:5,col=1:5)
screen(left[2])                                      # plot on the lower left par.
plot(6:10,col=6:10)
screen(2)                                             #plot on the  right part
plot(1:15,col=1:15)
close.screen(all=T)                            # close all the screens.

Back to top


Animation in R
Here is an animation example:
library(maps)
sname=c("Alabama","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia","Florida","Georgia","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine",
"Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada",
#"NewEngland",
"New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma",
"Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont",
"Virginia","Washington","West Virginia","Wisconsin","Wyoming")
map("usa")
title(sub="Hawaii and Alaska are not shown in this map.")
for(i in 1:length(sname))
{
    map(database="state",regions=sname[i],fill=TRUE,col=i,add=T)
    system("sleep 0.5")
}

Back to top


Install R packages ( for Linux)
For example: 

  1. Download package.
  2. Setup the directory for packages, say Rpackages, e.g.:   $mkdir ~/Rpackages
  3. Add environment variable. If using bash, add following line to ~/.bashrc:   export R_LIBS=~/Rpackages   .If using tcsh, add following line to ~/.cshrc:  setenv R_LIBS ~/Rpackages  . For the first time, you may need to source this file, e.g. for bash: $ source ~/.bashrc
  4. Move the package to ~/Rpackages
  5. Run R install command. For example, to install the fields package: $ R CMD INSTALL fields_1.2.tar.gz
  6. Start R and load the package. For example, for the fields package:  > library(fields)

Back to top


Set the same unit for x and y
Option, asp=1, is to set the aspect ratio as 1, e.g.,

plot(c(156,204),c(168,228),type="n",xlab="x (km)", ylab="y (km)",axes=FALSE,asp=1)

Back to top


Trellis graphics: the lattice package

The dataset, test, is in the format of data.frame with components x, y, g and z.  The plot of (x,y) is produced conditional on g. Therefore, several plots will be generated, showing y against x for each level of g. For each plot, several lines will be drawn for each level of z.

library(lattice)
xyplot(y~x|g,data=test,groups=z,panel=panel.superpose,type="l",lty=1:6, col=c(1,1,1,grey(5/8),1,grey(5/8)),
          scale=list(x=list(at=c(0,6,12,18,24))),
          key=list(space="bottom",lines=TRUE,lty=1:6,col=c(1,1,1,grey(5/8),1,grey(5/8)),
                       text=list(lab=as.character(unique(test$z))),columns=3),
          main="Hour")

When I first played around with this function, legend was messed up, due to the order of  the characters (test$z).  The order has to be pre-specified. For example,  

    a <- c("obs","M36","M12","A12","M04","A04")
    z <- factor(a,levels=unique(a))

By doing so, the order of z is specified as what's shown in a, not in the natural order.
Back to top

Back to Li's homepage
Last update 01/31/2007