Revision 3 as of 2010-06-08 19:43:44

Clear message

Write to the clipboard

Ever produce a list that you want to take somewhere else outside of R without having to write it to a file? Use the ClipBoard!

The only annoying thing is that you have to write them as a character vector.

# get some random numbers
x <- runif(5)
# write them to the clipboard
writeClipboard(as.character(x))

How to flatten a matrix in R

If you have a matrix or data frame where some of the rows have values that need to be averaged. There are a few ways to go about this.

Imagine you have a table of gene expression values measured under various conditions. Each row of the table represents a gene, some of the rows represent replicate measurements of that gene. The gene name is found in the column "sys_id". Each column represents a different condition. To average the replicates you could do the following:

Example 1:

library(stats)

mat <- aggregate(dat[, 2:5], list(dat$sys_id), mean)

Example 2: Create a factor with the column of gene names, then use that factor to apply a function to the values in each individual column, then recombine the columns into a dataframe or matrix.

use tapply()

tapply(data, factor, function)