<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Matrix on Chatomics</title>
    <link>https://divingintogeneticsandgenomics.com/tags/matrix/</link>
    <description>Recent content in Matrix on Chatomics</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <copyright>&amp;copy; 2018 Ming &#39;Tommy&#39; Tang</copyright>
    <lastBuildDate>Mon, 14 Apr 2014 13:54:00 -0700</lastBuildDate>
    
	<atom:link href="https://divingintogeneticsandgenomics.com/tags/matrix/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>order a matrix by a pre-defined group and then by the rowSums in R</title>
      <link>https://divingintogeneticsandgenomics.com/archive/order-a-matrix-by-a-pre-defined-group-and-then-by-the-rowsums-in-r/</link>
      <pubDate>Mon, 14 Apr 2014 13:54:00 -0700</pubDate>
      
      <guid>https://divingintogeneticsandgenomics.com/archive/order-a-matrix-by-a-pre-defined-group-and-then-by-the-rowsums-in-r/</guid>
      <description>I will lay down the R code below
#create a matrix
&amp;gt; mat&amp;lt;- matrix(sample.int(10, size=30, replace=T), ncol=3) &amp;gt; mat [,1] [,2] [,3] [1,] 4 7 6 [2,] 5 6 1 [3,] 4 3 1 [4,] 2 2 9 [5,] 4 5 4 [6,] 8 9 8 [7,] 5 9 7 [8,] 8 6 7 [9,] 3 8 5 [10,] 4 3 6  # column bind the mat and the sum of the rows together  &amp;gt; mat1&amp;lt;- cbind(mat, rowSums(mat)) &amp;gt; mat1 [,1] [,2] [,3] [,4] [1,] 4 7 6 17 [2,] 5 6 1 12 [3,] 4 3 1 8 [4,] 2 2 9 13 [5,] 4 5 4 13 [6,] 8 9 8 25 [7,] 5 9 7 21 [8,] 8 6 7 21 [9,] 3 8 5 16 [10,] 4 3 6 13 # order the matrix by the rowSums(mat) &amp;gt; mat[order(rowSums(mat)),] [,1] [,2] [,3] [1,] 4 3 1 [2,] 5 6 1 [3,] 2 2 9 [4,] 4 5 4 [5,] 4 3 6 [6,] 3 8 5 [7,] 4 7 6 [8,] 5 9 7 [9,] 8 6 7 [10,] 8 9 8 # compare with the order by looking at the last column &amp;gt; mat1[order(rowSums(mat)),] [,1] [,2] [,3] [,4] [1,] 4 3 1 8 [2,] 5 6 1 12 [3,] 2 2 9 13 [4,] 4 5 4 13 [5,] 4 3 6 13 [6,] 3 8 5 16 [7,] 4 7 6 17 [8,] 5 9 7 21 [9,] 8 6 7 21 [10,] 8 9 8 25 # order the mat matrix in a decreasing order by adding a minus sign &amp;gt; mat[order(-rowSums(mat)),] [,1] [,2] [,3] [1,] 8 9 8 [2,] 5 9 7 [3,] 8 6 7 [4,] 4 7 6 [5,] 3 8 5 [6,] 2 2 9 [7,] 4 5 4 [8,] 4 3 6 [9,] 5 6 1 [10,] 4 3 1 # label the rows into different groups &amp;gt; rownames(mat)&amp;lt;- c(&amp;quot;f&amp;quot;,&amp;quot;a&amp;quot;,&amp;quot;d&amp;quot;,&amp;quot;c&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;d&amp;quot;,&amp;quot;e&amp;quot;,&amp;quot;a&amp;quot;,&amp;quot;f&amp;quot;,&amp;quot;f&amp;quot;) &amp;gt; mat [,1] [,2] [,3] f 4 7 6 a 5 6 1 d 4 3 1 c 2 2 9 b 4 5 4 d 8 9 8 e 5 9 7 a 8 6 7 f 3 8 5 f 4 3 6 &amp;gt; rownames(mat1)&amp;lt;- c(&amp;quot;f&amp;quot;,&amp;quot;a&amp;quot;,&amp;quot;d&amp;quot;,&amp;quot;c&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;d&amp;quot;,&amp;quot;e&amp;quot;,&amp;quot;a&amp;quot;,&amp;quot;f&amp;quot;,&amp;quot;f&amp;quot;) &amp;gt; mat1 [,1] [,2] [,3] [,4] f 4 7 6 17 a 5 6 1 12 d 4 3 1 8 c 2 2 9 13 b 4 5 4 13 d 8 9 8 25 e 5 9 7 21 a 8 6 7 21 f 3 8 5 16 f 4 3 6 13 &amp;gt; groups&amp;lt;- factor(rownames(mat)) &amp;gt; groups [1] f a d c b d e a f f Levels: a b c d e f # order the matrix by group first, and then within the subgroups, order by the rowSums &amp;gt; mat1[order(groups,rowSums(mat)),] [,1] [,2] [,3] [,4] a 5 6 1 12 a 8 6 7 21 b 4 5 4 13 c 2 2 9 13 d 4 3 1 8 d 8 9 8 25 e 5 9 7 21 f 4 3 6 13 f 3 8 5 16 f 4 7 6 17 It is extremely useful when I want to order the matrix by a pre-defined way and then plot the heatmap with heatmap2 in gplots package.</description>
    </item>
    
  </channel>
</rss>