Professional Documents
Culture Documents
Data Structures
Data Structures
Data Structures
Topic & Structure of the lesson
- Matrices
- Arrays
- Data.frames
- List
• Matrices
• Arrays
• Frames
• List
Matrix columns
Vector
1 4 7
1 2 3 4 5 6 7 8 9
rows 2 5 8
number of rows 3 6 9
> v<-c(1,2,3,4,5,6,7,8,9)
row and column names
> a=matrix(v,nrow=3,ncol=3)
> a number of columns
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
rows 2 2 5 8
3 3 6 9
1 2 3
-An element at the row, column of a can be mth nth
1 1 4 7
accessed by the expression a[m, n].
2 2 5 8
> a [2, 3] # element at 2nd row, 3rd column
[1] 8 3 3 6 9
1 2 3
1 1 4 7
- The entire mth row can be extracted as a[m, ].
2 2 5 8
> a [2, ] # the 2nd row
[1] 2 5 8 3 3 6 9
1 2 3
- The entire nth column can be extracted as a[ ,n]. 1 1 4 7
> a [ ,3] # the 3rd column 2 2 5 8
[1] 7 8 9 3 3 6 9
CT038-3-2 Object Oriented Development
CT127-3-2-Programming with Java
for Data Analysis File I/O
Data Structures SlideSlide
8 of 855of 19
Combining Matrices
> B = matrix( c(2, 4, 3, 1, 5, 7), nrow=3, ncol=2)
>B
[,1] [,2]
[1,] 2 1
[2,] 4 5
[3,] 3 7
> C = matrix( c(0,0,1,1),nrow=2,ncol=2)
>C
[,1] [,2]
[1,] 0 1
[2,] 0 1
-Then we can combine the rows of B and C with rbind.
> rbind(B, C)
[,1] [,2]
[1,] 2 1
[2,] 4 5
[3,] 3 7
[4,] 0 1
[5,] 0 1
CT038-3-2 Object Oriented Development
CT127-3-2-Programming with Java
for Data Analysis File I/O
Data Structures SlideSlide
9 of 955of 19
Combining Matrices
> B = matrix( c(2, 4, 3, 1, 5, 7), nrow=3, ncol=2)
>B
[,1] [,2]
[1,] 2 1
[2,] 4 5
[3,] 3 7
> C = matrix( c(0,0,0),nrow=3,ncol=1)
>C
[,1]
[1,] 0
[2,] 0
[3,] 0
-Then we can combine the columns of B and C with cbind.
> cbind(B, C)
,,2 2 10 13 22
[,1] [,2]
[1,] 13 22 18 14 11 11
[2,] 11 11 30 17 15 33
[3,] 15 33
CT038-3-2 Object Oriented Development
CT127-3-2-Programming with Java
for Data Analysis File I/O
Data Structures SlideSlide
13 of1355of 19
Arrays
vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
data[1,4,1]
data[3,2,2]
df[2:3,]
df[3,2]
data[[1]] data[[2]]
data[[1]][1,2]
1D Vector List
nD Array
• Matrices
• Arrays
• Data Frames
• Lists