Tuesday, March 23, 2010

Programming in R

http://manuals.bioinformatics.ucr.edu/home/programming-in-r
http://www.r-cookbook.com/node/2
http://cran.r-project.org/doc/manuals/R-intro.html
http://www.statmethods.net/interface/io.html
http://biocodenv.com/wordpress/?p=15 http://biocodenv.com/wordpress/?p=15

Sample script: refillsMisses.R
{{{
#!/usr/bin/Rscript

args <- c('1min_interval_data_clean.txt', '1min_interval_refillsMisses.txt', '1min_interval_refillsMisses.png', 'mcf')
#args <- c('z', '1min_interval_refillsMisses.txt', 'z.png', 'perl')

infile <- args[1]
outfile <- args[2]
outplot <- args[3]
filter <- args[4]

# read in data from infile
inFrame <- data.frame(read.table(infile, header=1))
inFrame <- subset(inFrame, PROG==filter)

# calculations between select columns
miss <- inFrame['DATA_CACHE_MISSES']
acc <- inFrame['DATA_CACHE_ACCESSES']
ret <- inFrame['RETIRED_INSTRUCTIONS']
ref <- inFrame["DATA_CACHE_REFILLS.ALL"]
prog <- inFrame['PROG']

refMiss <- data.frame(ref-miss)
colnames(refMiss) <- "RefillsMinusMiss"

missAcc <- data.frame(miss/acc*refMiss)
colnames(missAcc) <- 'MissDivAccessMultDiff'

# create output data frame with calculated columns
outFrame <- data.frame(inFrame, refMiss, missAcc)

# write to outfile
write.table(outFrame, file=outfile, row.names=FALSE, quote=FALSE)

# plot
png(outplot)
missAccMat <- as.matrix(missAcc)[,1]
missMat <- as.matrix(miss)[,1]
retMat <- as.matrix(ret)[,1]

#http://www.astrostatistics.psu.edu/su09/lecturenotes/clus2.html
#scatterplot3d(missAccMat, missMat, retMat)

plot(missAccMat, retMat, col='red', pch=15)
}}}

# open plotting window
op <- par(mfrow=c(2, 2))

# draw histogram
hist(x)

Filtering without subset, subset seems to modify the input matrix
Is this what you want?

> tt <- matrix(1:20, ncol = 4)
> tt
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
> tt[tt[,1] < 3, ]
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17

No comments: