Document tree: home « code « r-snippets.html

This page was updated on 2010-03-30 (NZST) and is tagged r, statistics, programming.

Some useful R snippets

Maintaining order when aggregating data

Data aggregation by factors by default uses alphanumeric order of the levels. However, some times I need to keep the original order of the data. For example, I may have many progeny trials in a dataset and those trials are ordered according an environmental gradient, rather than following an alphabetic order.

The following code uses unique to extract the levels of a factor in the occurring order. This is then assigned as the new levels of a factor.

dat$Trial <- factor(dat$Trial_id, levels = unique(dat$Trial_id))

Uppercase factors

In data sets I use lowercase names for continuous variables and uppercase first-letter for factors. R will transform alphanumeric variables to factors but if the factor, say replicate, uses numeric levels will not do the transformation. I use the following function for transformation:

cap.factors <- function(data) {
  # Gets text name of dataset
  dat.name <- substitute(data)

  # Loops over names of dataset
  # and extracts the ones that start with uppercase
  for(var.name in names(data)){
    if(substr(var.name, 1, 1) %in% LETTERS){
      left <- paste(dat.name, '$', var.name, sep = '')
      right <- paste('factor(', left, ')', sep = '')
      code <- paste(left, '<-', right)

      # Evaluates the parsed text, using the parent environment
      # so as to actually update the original data set
      eval(parse(text = code), envir = parent.frame())
    }
  }
}

So one first reads a data set and then uses the function as:

barley <- read.csv('~/Documents/Examples/barley.csv', header = TRUE)
cap.factors(barley)

Black & White figures in R

I was creating some trellis plots using R and wanted everything in black and white. I was playing with the xyplot command, setting options, fixing all the little details (even discovering some data issues in the process). Then, I decided to save them as PDF, when I discovered that, duh!, pdf() always saves everything as colour. Before thinking on saving the files I was using trellis.device(color = FALSE), but it did not make a difference for exporting as PDF.

Looking in the R email lists I found that one has to use trellis.device() after calling pdf() and force it to avoid creating a new canvas. Something like this:

pdf('~/Documents/Research/2007/Algae/temp05degrees.pdf',
     width = 11.7, height = 8.26, pointsize = 9)
trellis.device(new = FALSE, color = FALSE)

# and then the graph
xyplot(percent ~ time | temp:light.level:humidity, 
       group = tray, 
       type = 'b', 
       data = rocks[rocks$temp == 5,],
       xlab = 'Exposure [hours]', 
       ylab = 'Percentage live [%]',
       layout = c(4,2))
dev.off()

which works perfectly.