0% found this document useful (0 votes)
29 views

Create and Name Matrices

This document introduces matrices in R. It discusses how to create matrices using the matrix() function, with options to specify the number of rows and columns. It also covers binding matrices together using rbind() and cbind(), as well as naming rows and columns of a matrix using rownames() and colnames(). The document demonstrates coercing between numeric and character matrices and explains they contain different types so should be stored as a list or data frame.

Uploaded by

seggy7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Create and Name Matrices

This document introduces matrices in R. It discusses how to create matrices using the matrix() function, with options to specify the number of rows and columns. It also covers binding matrices together using rbind() and cbind(), as well as naming rows and columns of a matrix using rownames() and colnames(). The document demonstrates coercing between numeric and character matrices and explains they contain different types so should be stored as a list or data frame.

Uploaded by

seggy7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

INTRODUCTION TO R

Create and Name


Matrices

Introduction to R

Matrix

Vector: 1D array of data elements

Matrix: 2D array of data elements

Rows and columns

One atomic vector type

Introduction to R

Create a matrix

matrix()

> matrix(1:6, nrow = 2)


[,1] [,2] [,3]
[1,]
1
3
5
[2,]
2
4
6
> matrix(1:6, ncol = 3)
[,1] [,2] [,3]
[1,]
1
3
5
[2,]
2
4
6
> matrix(1:6, nrow = 2, byrow = TRUE)
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
4
5
6

Introduction to R

Create a matrix: recycling


> matrix(1:3, nrow = 2, ncol = 3)
[,1] [,2] [,3]
[1,]
1
3
2
[2,]
2
1
3
> matrix(1:4, nrow = 2, ncol = 3)
[,1] [,2] [,3]
[1,]
1
3
1
[2,]
2
4
2
Warning message:
In matrix(1:4, nrow = 2, ncol = 3) :
data length [4] is not a sub-multiple or multiple of the
number of columns [3]

Introduction to R

rbind(), cbind()
> cbind(1:3, 1:3)
[,1] [,2]
[1,]
1
1
[2,]
2
2
[3,]
3
3
> rbind(1:3, 1:3)
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
1
2
3

Introduction to R

rbind(), cbind()
> m <- matrix(1:6, byrow = TRUE, nrow = 2)
> rbind(m, 7:9)
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
4
5
6
[3,]
7
8
9
> cbind(m, c(10, 11))
[,1] [,2] [,3] [,4]
[1,]
1
2
3
10
[2,]
4
5
6
11

Introduction to R

Naming a matrix rownames(), colnames()


> m <- matrix(1:6, byrow = TRUE, nrow = 2)
> rownames(m) <- c("row1", "row2")
> m

[,1] [,2] [,3]


row1
1
2
3
row2
4
5
6
> colnames(m) <- c("col1", "col2", "col3")
> m
col1 col2 col3
row1
1
2
3
row2
4
5
6

Introduction to R

Naming a matrix
> m <- matrix(1:6, byrow = TRUE, nrow = 2)

Introduction to R

Naming a matrix
> m <- matrix(1:6, byrow = TRUE, nrow = 2,
dimnames = list(c("row1", "row2"),
c("col1", "col2", "col3")))
> m

col1 col2 col3


row1
1
2
3
row2
4
5
6

Introduction to R

Coercion
> num <- matrix(1:8, ncol = 2)
> num
[,1] [,2]
[1,]
1
5
[2,]
2
6
[3,]
3
7
[4,]
4
8
> char <- matrix(LETTERS[1:6], nrow = 4, ncol = 3)
> char
[,1] [,2] [,3]
[1,] "A" "E" "C"
[2,] "B" "F" "D"
[3,] "C" "A" "E"
[4,] "D" "B" "F"

Introduction to R

Coercion
> num <- matrix(1:8, ncol = 2)
> char <- matrix(LETTERS[1:6], nrow = 4, ncol = 3)
> cbind(num, char)
[,1] [,2] [,3]
[1,] "1" "5" "A"
[2,] "2" "6" "B"
[3,] "3" "7" "C"
[4,] "4" "8" "D"

[,4]
"E"
"F"
"A"
"B"

[,5]
"C"
"D"
"E"
"F"

Contain dierent types? list or data.frame

INTRODUCTION TO R

Lets practice!

You might also like