User:Timothee Flutre/Notebook/Postdoc/2012/02/17: Difference between revisions
From OpenWetWare
(Autocreate 2012/02/17 Entry for User:Timothee_Flutre/Notebook/Postdoc) |
(→Entry title: in R, subtract vector from columns of matrix) |
||
Line 6: | Line 6: | ||
| colspan="2"| | | colspan="2"| | ||
<!-- ##### 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. ##### --> | ||
== | ==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 the row of a matrix, or from its column, especially when the matrix is square. | |||
* 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 | |||
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: | |||
m - v | |||
[,1] [,2] | |||
[1,] 0 1 | |||
[2,] -2 -1 | |||
We could also have done it with [http://stat.ethz.ch/R-manual/R-patched/library/base/html/apply.html apply]: | |||
apply(m, 2, "-", v) | |||
[,1] [,2] | |||
[1,] 0 1 | |||
[2,] -2 -1 | |||
Or with [http://stat.ethz.ch/R-manual/R-patched/library/base/html/sweep.html 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... | |||
<!-- ##### 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 15:33, 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> </html>Next entry<html><img src="/images/5/5c/Resultset_next.png" border="0" /></html> |
How to subtract a vector from a matrix in RIn 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 the row of a matrix, or from its column, especially when the matrix is square.
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
[,1] [,2] [1,] 0 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: 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... |