Twitter

Tuesday, November 13, 2007

Temp Directory and Working Director of R

For a while, I have been using the setwd() command at the beginning of a project's code. This allows one to save data, graph, output easily to a project's result folder without typing long path names.

Yesterday, I was using a contributed package in R that requires the access to R's temp directory. The exact location of this folder can be found out using the command tempdir(). It just happened that my current location of this folder was not ideal for the task at hand and I needed to change it. There was no command in R to change it. I figured out a way to do it: add an entry to the environmental variable list of the system as TMPDIR and specify the desired folder name.

Monday, November 12, 2007

Useful websites for graphics in R

R graphical manuals (a nice collection of examples and codes)
http://cged.genes.nig.ac.jp/RGM2/index.php?clear=all

Codes from the book "R graphics"
http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html

Friday, November 02, 2007

Fast permuting r by c two-way tables

Today, I need to permute many two-way tables of the same dimension (say 2 by 3) to carry out a test of independence. My data looks like a big matrix with each row corresponding to a two-way table (arranged by row). The permutation is just to draw a random 2 by 3 table given independence between the two dimensions, conditioning on the observed marginal distributions. I didn't find (or didn't have time to find) a function to do that in R. Therefore I wrote the following codes:

f.perm.tab<-function(x){

x.r<-rep(1:nrow(x), times=rowSums(x))

x.c<-rep(1:ncol(x), times=colSums(x))

temp.mat<-expand.grid(1:nrow(x), 1:ncol(x))

return(table(c(x.r, temp.mat[,1]), c(sample(x.c),temp.mat[,2])) -matrix(1, nrow(x), ncol(x)))

}


I also made a function to do the permutation for each line of my huge
matrix and used the apply() function to speed up things. It works pretty well
for me.