### R Matrix

R matrix is a two dimensional array. R has a lot of operator and functions that make matrix handling very convenient.

Matrix assignment:

>A <- matrix(c(3,5,7,1,9,4),nrow=3,ncol=2,byrow=TRUE) >A

[,1] [,2] [1,] 3 5 [2,] 7 1 [3,] 9 4

Matrix row and column count:

>rA <- nrow(A) >rA

[1] 3

>cA <- ncol(A) >cA

[1] 2

`t(A)`

function returns a transposed matrix of A:>B <- t(A) >B

[,1] [,2] [,3] [1,] 3 7 9 [2,] 5 1 4

Matrix multplication:

C <- A * A C

[,1] [,2] [1,] 9 25 [2,] 49 1 [3,] 81 16

Matrix Addition:

>C <- A + A >C

[,1] [,2] [1,] 6 10 [2,] 14 2 [3,] 18 8

Matrix subtraction (-) and division (/) operations ... ...

Sometimes a matrix need to be sorted by a specific column, which can be done by using

`order()`

function.
Following is a csv file example.

Following R code will read in the above file into a matrix, and sort it by column 4, then write to a output file.

The result is:

Download the csv file and the R source code:

__Data File__

__R Source Code File__

Order() returns a permutation which rearranges its first argument
into ascending or descending order, breaking ties by further
arguments.

**Usage:**

order(..., na.last = TRUE, decreasing = FALSE)

**Arguments:**

...: a sequence of numeric, complex, character or logical vectors,
all of the same length, or a classed R object.

decreasing: logical. Should the sort order be increasing or
decreasing?

na.last: for controlling the treatment of 'NA's. If 'TRUE', missing
values in the data are put last; if 'FALSE', they are put
first; if 'NA', they are removed.