User:Timothee Flutre/Notebook/Postdoc/2011/11/09

From OpenWetWare

(Difference between revisions)
Jump to: navigation, search
(Entry title: visualize NA in matrix)
(Entry title: use R built-in "match")
Line 16: Line 16:
  rownames(mat) <- new.rownames
  rownames(mat) <- new.rownames
-
Or it's maybe easier with the built-in [http://stat.ethz.ch/R-manual/R-patched/library/base/html/replace.html replace] function.
+
Or it's maybe easier with the built-in [http://stat.ethz.ch/R-manual/R-patched/library/base/html/match.html match] function:
 +
 
 +
new.rownames <- links$id2[ match(rownames(mat), links$id1) ]

Revision as of 13:18, 10 November 2011

Project name Main project page
Previous entry      Next entry

Entry title

  • in R, replace the row names of a matrix by their new names when the order is different (but assuming one-to-one mapping):
links <- data.frame(id1=c("a","b","c"), id2=c("1","2","3"), stringsAsFactors=FALSE)
mat <- matrix(runif(3*10), nrow=3)
rownames(mat) <- c("b","c","a")
new.rownames <- unlist(lapply(rownames(mat), function(i){links$id2[which(links$id1 == i)]}))
rownames(mat) <- new.rownames

Or it's maybe easier with the built-in match function:

new.rownames <- links$id2[ match(rownames(mat), links$id1) ]


  • in R, visualize the missing values (encoded as "NA") in a matrix:
image(1:nrow(mat), 1:ncol(mat), is.na(mat), col=c("white","black"),
      main="Missing values", xlab="Genes", ylab="Samples")


Personal tools