> # create a 4x3 matrix > a=matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=4) > a [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12 > #arrange the numbers by row > a=matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=4, byrow=T) > a [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 > # Combine 2x2 matrix by row and column > e = matrix(c(1,2,3,4),nrow=2) > f = matrix(c(5,6,7,8),nrow=2) > e [,1] [,2] [1,] 1 3 [2,] 2 4 > f [,1] [,2] [1,] 5 7 [2,] 6 8 > cbind(e,f) [,1] [,2] [,3] [,4] [1,] 1 3 5 7 [2,] 2 4 6 8 > rbind(e,f) [,1] [,2] [1,] 1 3 [2,] 2 4 [3,] 5 7 [4,] 6 8 > # Take a single digit subset from Matrix a Row 3 Column 2 > a[3,2] [1] 8 > # Take a 2x3 subset of Matrix a, change the numbers and then combine back in to the original. > a[c(3,4),c(1,2,3)] [,1] [,2] [,3] [1,] 7 8 9 [2,] 10 11 12 > a[c(3,4),c(1,2,3)] = matrix(c(1,2,3,4,5,6),nrow=2) > a [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 1 3 5 [4,] 2 4 6
Created by Pretty R at inside-R.org
More here
No comments:
Post a Comment