Difference between revisions of "User:Timothee Flutre/Notebook/Postdoc/2011/11/09"

From OpenWetWare
Jump to: navigation, search
(Autocreate 2011/11/09 Entry for User:Timothee_Flutre/Notebook/Postdoc)
 
(Entry title: replace rownames with mapping when different order)
Line 7: Line 7:
 
<!-- ##### DO NOT edit above this line unless you know what you are doing. ##### -->
 
<!-- ##### DO NOT edit above this line unless you know what you are doing. ##### -->
 
==Entry title==
 
==Entry title==
* Insert content here...
 
  
 +
* 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
  
 
<!-- ##### DO NOT edit below this line unless you know what you are doing. ##### -->
 
<!-- ##### DO NOT edit below this line unless you know what you are doing. ##### -->

Revision as of 11:47, 9 November 2011

Owwnotebook icon.png Project name <html><img src="/images/9/94/Report.png" border="0" /></html> Main project page
<html><img src="/images/c/c3/Resultset_previous.png" border="0" /></html>Previous entry<html>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</html>Next entry<html><img src="/images/5/5c/Resultset_next.png" border="0" /></html>

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