User:Timothee Flutre/Notebook/Postdoc/2012/02/17: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
Line 9: Line 9:


In R, matrix algebra is very fast, so it's better to use it. But it's not always clear how to subtract a vector from each row of a matrix, or from each column, especially when the matrix is square. (When the matrix is square, R will always successfully return you an answer but it may not correspond to what you ask! While if the matrix is not square, R will run successfully or not, depending on the length of the vector).
In R, matrix algebra is very fast, so it's better to use it. But it's not always clear how to subtract a vector from each row of a matrix, or from each column, especially when the matrix is square. (When the matrix is square, R will always successfully return you an answer but it may not correspond to what you ask! While if the matrix is not square, R will run successfully or not, depending on the length of the vector).
* Key fact: internally, '''R records a matrix as a long vector corresponding to the concatenation of the columns''' ([http://en.wikipedia.org/wiki/Vectorization_%28mathematics%29 vectorization] in maths).


* Before anything else, let's define our dummy data. Here is my matrix:
* Before anything else, let's define our dummy data. Here is my matrix:
Line 30: Line 32:
  [2,]  -2  -1
  [2,]  -2  -1


Internally, R represents a matrix as a vector concatenating the columns of the matrix. That's why the following simple command gives me what I want:
The following simple command gives me what I want:


  m - v
  m - v

Revision as of 15:56, 17 February 2012

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>

How to subtract a vector from a matrix in R

In R, matrix algebra is very fast, so it's better to use it. But it's not always clear how to subtract a vector from each row of a matrix, or from each column, especially when the matrix is square. (When the matrix is square, R will always successfully return you an answer but it may not correspond to what you ask! While if the matrix is not square, R will run successfully or not, depending on the length of the vector).

  • Key fact: internally, R records a matrix as a long vector corresponding to the concatenation of the columns (vectorization in maths).
  • Before anything else, let's define our dummy data. Here is my matrix:
m <- matrix(1:4, nrow=2, ncol=2, byrow=TRUE)
m
     [,1] [,2]
[1,]    1    2
[2,]    3    4

And here is my vector:

v <- c(1, 5)
v
[1] 1 5
  • First, I want to subtract v from each column of m. Here is what I want to obtain:
     [,1] [,2]
[1,]    0    1
[2,]   -2   -1

The following simple command gives me what I want:

m - v
     [,1] [,2]
[1,]    0    1
[2,]   -2   -1

We could also have done it with apply:

apply(m, 2, "-", v)
     [,1] [,2]
[1,]    0    1
[2,]   -2   -1

Or with sweep:

sweep(m, 1, v, "-")
     [,1] [,2]
[1,]    0    1
[2,]   -2   -1

Note that the parameter MARGIN in "apply" should be put to 2 to operate on the columns, while in "sweep" it should be put to 1...

  • Second, I want to subtract v from each row of m. Here is what I want to obtain:
     [,1] [,2]
[1,]    0   -3
[2,]    2   -1

Of course, m - v won't work here. But again, as we know how R handle matrices internally, we can use the transpose function:

t(t(m)-v)
     [,1] [,2]
[1,]    0   -3
[2,]    2   -1

We could also have use "apply" (with MARGIN=1). But wait, "transpose" is also required here!

t(apply(m, 1, "-", v))
     [,1] [,2]
[1,]    0   -3
[2,]    2   -1

And for "sweep"? Well, it understands properly that we want to operate on rows (MARGIN=2), so it returns the results row by row...

sweep(m, 2, v, "-")
     [,1] [,2]
[1,]    0   -3
[2,]    2   -1