- In R, vector includes: atomic vector and list.
double
means regular number.double
data type is also called numeric
.4.5
## [1] 4.5
is.vector(4.5)
## [1] TRUE
typeof(4.5)
## [1] "double"
die <- c(1, 2, 3, 4, 5, 6) die
## [1] 1 2 3 4 5 6
die
is a double vector of length 6.typeof(die)
## [1] "double"
typeof()
returns the type of the data (double, integer, character, complex, or raw).integer
refers to numbers that can be written without a decimal component.double
.integer
:my_integer <- c(-1L, 2L, 4L) my_integer
## [1] -1 2 4
typeof(my_integer)
## [1] "integer"
text <- c("Hello", 'World') text
## [1] "Hello" "World"
typeof(text)
## [1] "character"
TRUE
and FALSE
, also called Boolean data type.TRUE
= 1 and FALSE
= 0.my_logic <- c(TRUE, TRUE, FALSE) my_logic
## [1] TRUE TRUE FALSE
sum(my_logic)
## [1] 2
typeof(my_logic)
## [1] "logical"
my_complex <- complex(real = 1, imaginary = 2) my_complex
## [1] 1+2i
typeof(my_complex)
## [1] "complex"
my_raw <- raw(length = 3) my_raw
## [1] 00 00 00
typeof(my_raw)
## [1] "raw"
Atomic Vectors | Lists |
---|---|
- Group data into a one-dimensional array | - Group data into a one-dimensional array |
- Group individual values of the same data type | - Group R objects such as atomic vectors and other lists |
my_vector <- c(1, "one", TRUE) my_vector
## [1] "1" "one" "TRUE"
my_list <- list(1, "one", TRUE) my_list
## [[1]] ## [1] 1 ## ## [[2]] ## [1] "one" ## ## [[3]] ## [1] TRUE
list()
creates a list in the same way c()
creates a vector.big_list <- list(1:12, "2020", list("january", 2:8)) big_list
## [[1]] ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 ## ## [[2]] ## [1] "2020" ## ## [[3]] ## [[3]][[1]] ## [1] "january" ## ## [[3]][[2]] ## [1] 2 3 4 5 6 7 8
another_list <- list(c("stat385", "spring2020"), 1:5, list("homework", "projects")) another_list
## [[1]] ## [1] "stat385" "spring2020" ## ## [[2]] ## [1] 1 2 3 4 5 ## ## [[3]] ## [[3]][[1]] ## [1] "homework" ## ## [[3]][[2]] ## [1] "projects"
instructor <- list("Ha", 24) students <- list(list("Alex", 20), list("Dave", 21)) stat385 <- list(instructor, students)
stat385
have?stat385
## [[1]] ## [[1]][[1]] ## [1] "Ha" ## ## [[1]][[2]] ## [1] 24 ## ## ## [[2]] ## [[2]][[1]] ## [[2]][[1]][[1]] ## [1] "Alex" ## ## [[2]][[1]][[2]] ## [1] 20 ## ## ## [[2]][[2]] ## [[2]][[2]][[1]] ## [1] "Dave" ## ## [[2]][[2]][[2]] ## [1] 21
big_list
## [[1]] ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 ## ## [[2]] ## [1] "2020" ## ## [[3]] ## [[3]][[1]] ## [1] "january" ## ## [[3]][[2]] ## [1] 2 3 4 5 6 7 8
big_list[1]
## [[1]] ## [1] 1 2 3 4 5 6 7 8 9 10 11 12
big_list[1]
returns a list of length 1 containing an atomic vector of type double.big_list[[1]]
## [1] 1 2 3 4 5 6 7 8 9 10 11 12
big_list[[1]]
returns an atomic vector of type double.
[[1]]
means list.[1]
means vector.another_list[[1]]
## [1] "stat385" "spring2020"
another_list[[1]][1]
## [1] "stat385"
another_list[1]
## [[1]] ## [1] "stat385" "spring2020"
another_list[1][1]
## [[1]] ## [1] "stat385" "spring2020"
stat385
## [[1]] ## [[1]][[1]] ## [1] "Ha" ## ## [[1]][[2]] ## [1] 24 ## ## ## [[2]] ## [[2]][[1]] ## [[2]][[1]][[1]] ## [1] "Alex" ## ## [[2]][[1]][[2]] ## [1] 20 ## ## ## [[2]][[2]] ## [[2]][[2]][[1]] ## [1] "Dave" ## ## [[2]][[2]][[2]] ## [1] 21
[[]]
and []
, we can access any element in a list.stat385[[2]]
## [[1]] ## [[1]][[1]] ## [1] "Alex" ## ## [[1]][[2]] ## [1] 20 ## ## ## [[2]] ## [[2]][[1]] ## [1] "Dave" ## ## [[2]][[2]] ## [1] 21
stat385[[2]][[1]]
## [[1]] ## [1] "Alex" ## ## [[2]] ## [1] 20
stat385[[2]][[1]][[1]]
## [1] "Alex"
stat385
?stat385
## [[1]] ## [[1]][[1]] ## [1] "Ha" ## ## [[1]][[2]] ## [1] 24 ## ## ## [[2]] ## [[2]][[1]] ## [[2]][[1]][[1]] ## [1] "Alex" ## ## [[2]][[1]][[2]] ## [1] 20 ## ## ## [[2]][[2]] ## [[2]][[2]][[1]] ## [1] "Dave" ## ## [[2]][[2]][[2]] ## [1] 21
stat385
?stat385[[1]]
## [[1]] ## [1] "Ha" ## ## [[2]] ## [1] 24
stat385[[1]][[2]]
## [1] 24
m <- matrix(data = die, nrow = 2) m
## [,1] [,2] [,3] ## [1,] 1 3 5 ## [2,] 2 4 6
matrix()
with an atomic vector to reorganize into a matrix and specify the number of rows/columns.m <- matrix(data = die, ncol = 3) m
## [,1] [,2] [,3] ## [1,] 1 3 5 ## [2,] 2 4 6
m <- matrix(data = die, nrow = 2, ncol = 2) m
## [,1] [,2] ## [1,] 1 3 ## [2,] 2 4
matrix()
will fill up the matrix column by column. We can fill the matrix row by row by including the argument byrow = TRUE
:m <- matrix(data = die, nrow = 2, byrow = TRUE) m
## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 4 5 6
[ , ]
:m
## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 4 5 6
m[1, 1]
## [1] 1
m[2, 2]
## [1] 5
[ , ]
:m
## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 4 5 6
m[1, ]
## [1] 1 2 3
m[, 3]
## [1] 3 6
[ , ]
:m
## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 4 5 6
m[1:2, 1:2]
## [,1] [,2] ## [1,] 1 2 ## [2,] 4 5
[ , ]
:m
## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 4 5 6
m[1, c(1, 3)]
## [1] 1 3
array()
function creates \(n\)-dimensional array.my_array <- array(data = 1:12, dim = c(2, 6)) my_array
## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] 1 3 5 7 9 11 ## [2,] 2 4 6 8 10 12
my_array <- array(data = 1:14, dim = c(2, 6)) my_array
## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] 1 3 5 7 9 11 ## [2,] 2 4 6 8 10 12
my_array
is a two-dimensional array, which is a matrix.my_array <- array(data = 1:12, dim = c(2, 2, 3)) my_array
## , , 1 ## ## [,1] [,2] ## [1,] 1 3 ## [2,] 2 4 ## ## , , 2 ## ## [,1] [,2] ## [1,] 5 7 ## [2,] 6 8 ## ## , , 3 ## ## [,1] [,2] ## [1,] 9 11 ## [2,] 10 12
[ , , ]
:my_array[1, 1, 1]
## [1] 1
my_array[ , , 1]
## [,1] [,2] ## [1,] 1 3 ## [2,] 2 4
my_array[ , 1, 1]
## [1] 1 2
attributes()
to see which attributes an object has.attributes(die)
## NULL
NULL
means the object has no attribute.names()
is the helper function associated with the names attribute.names(die)
## NULL
names(die) <- c("one", "two", "three", "four", "five", "six") attributes(die)
## $names ## [1] "one" "two" "three" "four" "five" "six"
die
## one two three four five six ## 1 2 3 4 5 6
die + 1
## one two three four five six ## 2 3 4 5 6 7
names(die) <- c("small", "small", "small", "big", "big", "big") die
## small small small big big big ## 1 2 3 4 5 6
names(die) <- NULL die
## [1] 1 2 3 4 5 6
dim()
returns the dimension of an R Object.dim(die)
## NULL
dim(die) <- c(2, 3) die
## [,1] [,2] [,3] ## [1,] 1 3 5 ## [2,] 2 4 6