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)

R/Misc (last edited 2019-06-14 16:18:29 by ChrisSeidel)