10 Data Structures
Common Data Structures:
- Vectors
- 1 Dimensional
- Homogenous data type
- Matrices
- 2 Dimensional
- Homogenous data type
- Arrays
- Greater than 2 Dimensions
- Homogenous data type
- Data Frames
- 2 Dimensional
- Heterogenous data types
- Lists
- Can contain various data types (vectors, matrices, arrays, and data frames)
- Heterogenous data types
10.1 Vectors
- 1 Dimensional
- Homogenous data type
Numeric vector
c(1, 2, 3, 4, 5)
## [1] 1 2 3 4 5
Character Vector
c("A", "B", "C", "D", "E")
## [1] "A" "B" "C" "D" "E"
Logical vector
c(TRUE, FALSE, TRUE, FALSE, TRUE)
## [1] TRUE FALSE TRUE FALSE TRUE
10.2 Matrices
- 2 Dimensional
- Homogenous data type
matrix(c(1:5, 2, 7, 4, 9, 20, 40, 23, 64, 67, 80, 3, 76, 29, 59, 91),
ncol = 4)
## [,1] [,2] [,3] [,4]
## [1,] 1 2 40 3
## [2,] 2 7 23 76
## [3,] 3 4 64 29
## [4,] 4 9 67 59
## [5,] 5 20 80 91
10.3 Arrays
- Greater than 2 Dimensions
- Homogenous data type
array(c(c(1:5, 2, 7, 4, 9, 20, 40, 23, 64, 67, 80, 3, 76, 29, 59, 91, 1, 3, 5, 7, 9),
c(2, 3, 34, 45, 57, 26, 74, 42, 91, 20, 1, 0, 82, 31, 45, 1, 0, 1, 0, 1, 9:5),
c(1, 0, rep(1, 3), rep(0, 5), 1, 0, 1, 1, 0, rep(1, 5), 0, 0, 1, 1, 0)),
c(5, 5, 3))
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 40 3 1
## [2,] 2 7 23 76 3
## [3,] 3 4 64 29 5
## [4,] 4 9 67 59 7
## [5,] 5 20 80 91 9
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2 26 1 1 9
## [2,] 3 74 0 0 8
## [3,] 34 42 82 1 7
## [4,] 45 91 31 0 6
## [5,] 57 20 45 1 5
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 1 1 0
## [2,] 0 0 0 1 0
## [3,] 1 0 1 1 1
## [4,] 1 0 1 1 1
## [5,] 1 0 0 1 0
10.4 Data Frames
- 2 Dimensional
- Heterogenous data types
data.frame(
Numeric = c(1, 2, 3, 4, 5),
Charcter = c("A", "B", "C", "D", "E"),
Logical = c(TRUE, FALSE, TRUE, FALSE, TRUE),
stringsAsFactors = FALSE
)
## Numeric Charcter Logical
## 1 1 A TRUE
## 2 2 B FALSE
## 3 3 C TRUE
## 4 4 D FALSE
## 5 5 E TRUE
10.5 Lists
- Can contain various data types (Vectors, Matrices, Arrays, and Data Frames)
- Heterogenous data types
list(
c(TRUE, FALSE, TRUE, FALSE, TRUE),
matrix(c("A", "B", "C", "D", "E",
"AB", "BC", "CD", "DE", "EF",
rep("BLAH", 5),
"TEXT", "text", "TEXT", "text", "TEXT"),
ncol = 4),
array(c(c(1:5, 2, 7, 4, 9, 20, 40, 23, 64, 67, 80, 3, 76, 29, 59, 91, 1, 3, 5, 7, 9),
c(2, 3, 34, 45, 57, 26, 74, 42, 91, 20, 1, 0, 82, 31, 45, 1, 0, 1, 0, 1, 9:5),
c(1, 0, rep(1, 3), rep(0, 5), 1, 0, 1, 1, 0, rep(1, 5), 0, 0, 1, 1, 0)),
c(5, 5, 3)),
data.frame(
Numeric = c(1, 2, 3, 4, 5),
Charcter = c("A", "B", "C", "D", "E"),
Logical = c(TRUE, FALSE, TRUE, FALSE, TRUE),
stringsAsFactors = FALSE
)
)
## [[1]]
## [1] TRUE FALSE TRUE FALSE TRUE
##
## [[2]]
## [,1] [,2] [,3] [,4]
## [1,] "A" "AB" "BLAH" "TEXT"
## [2,] "B" "BC" "BLAH" "text"
## [3,] "C" "CD" "BLAH" "TEXT"
## [4,] "D" "DE" "BLAH" "text"
## [5,] "E" "EF" "BLAH" "TEXT"
##
## [[3]]
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 40 3 1
## [2,] 2 7 23 76 3
## [3,] 3 4 64 29 5
## [4,] 4 9 67 59 7
## [5,] 5 20 80 91 9
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2 26 1 1 9
## [2,] 3 74 0 0 8
## [3,] 34 42 82 1 7
## [4,] 45 91 31 0 6
## [5,] 57 20 45 1 5
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 1 1 0
## [2,] 0 0 0 1 0
## [3,] 1 0 1 1 1
## [4,] 1 0 1 1 1
## [5,] 1 0 0 1 0
##
##
## [[4]]
## Numeric Charcter Logical
## 1 1 A TRUE
## 2 2 B FALSE
## 3 3 C TRUE
## 4 4 D FALSE
## 5 5 E TRUE