You are on page 1of 1

You can have a matrix of numbers:

5 30 55 80
10 35 60 85
15 40 65 90
20 45 70 95
25 50 75 100
or a matrix of character strings:
“Moe” “Larry” “Curly” “Shemp”
“Groucho” “Harpo” “Chico” “Zeppo”
“Ace” “King” “Queen” “Jack”
The numbers constitute a 5 (rows) X 4 (columns) matrix; the character strings
matrix is 3 X 4.
To create the 5 X 4 numerical matrix, first you create the vector of numbers from 5
to 100 in steps of 5:
> num_matrix <- seq(5,100,5)
Then you use the dim() function to turn the vector into a 2-dimensional matrix:
> dim(num_matrix) <-c(5,4)
> num_matrix
[,1] [,2] [,3] [,4]
[1,] 5 30 55 80
[2,] 10 35 60 85
[3,] 15 40 65 90
[4,] 20 45 70 95
[5,] 25 50 75 100
Note how R displays the bracketed row numbers along the side, and the bracketed
column numbers along the top.
Transposing a matrix interchanges the rows with the columns. In R, the t() func-
tion takes care of that:
> t(num_matrix)
[,1] [,2] [,3] [,4] [,5]

You might also like