Revision 16 as of 2013-10-08 15:21:31

Clear message

<<TableOfContents(3)>>

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))

To move tables of data, use the write.table function:

mytable <- matrix(1:100, ncol=10, nrow=10)
write.table(mytable, "clipboard", sep="\t")
# suppress row and column names
write.table(mytable, "clipboard", sep="\t", row.names=FALSE, col.names=FALSE)

To move a column of numbers into R from other programs you can use the scan() function. For instance

# bring a column of numbers from the clipboard into a vector
x <- scan()
# hit Paste, then Return
# or use readClipboard
x <- readClipboard()

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)

Colors for Heatmaps

library(RColorBrewer)
hmcol <- colorRampPalette(brewer.pal(10, "RdBu"))(256)

Write lines to a file

# open a file connection
fout <- file("filename", "w")
# print stuff to the connection (usually as part of a loop)
cat("some data", sep="\n", file=fout)
# close the connection
close(fout)

Install package from bioconductor

source("http://bioconductor.org/biocLite.R")
biocLite("ChIPpeakAnno")

List functions and objects in a package

ls("package:BioString")

# lists functions with call sequences:
lsf.str("package:Biostrings")

Divide each column in a matrix by it Column Sums

Use a function called sweep.

# create a matrix of random numbers
m <- matrix(sample(1:9,9),nrow=3,ncol=3)

# divide each column by column sums
sweep(m,2,colSums(m),"/")

Another way to do this would be use the prop.table function, which takes a "1" or "2" to denote by Row or by Column respectively.

prop.table(m,2)

Create an Empty Dataframe

Sometimes you want to create a data structure to populate, with pre-defined data types. How do you do that in R, in the absence of any data?

mydf <- data.frame(fruit=character(), quantity=numeric(), stringsAsFactors=FALSE)