Vectors
- factors are built on top of integer vectors;
- dates and date-times are built on top of numeric vectors;
- data frames and tibbles are built on top of lists
## [1] "character"
## [1] "integer"
#length
x <- list("a", "b", 1:10)
x
## [[1]]
## [1] "a"
##
## [[2]]
## [1] "b"
##
## [[3]]
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] 3
#atomic vector
#logical
1:10%%3==0
## [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
## [1] "double"
typeof(1L) #make an integer
## [1] "integer"
## [1] 1.5
#doubles are approximations
#use dplyr::near() to allow for some numerical tolerance
c(-1,0,1)/0
## [1] -Inf NaN Inf
#character
#missing values
#atomic vectors
#as.logical, integer, double, character
x <- sample(20, 100, replace = T)
y <- x > 10
y
## [1] TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
## [20] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE
## [39] TRUE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE
## [58] FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE
## [77] FALSE TRUE TRUE FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE FALSE
## [96] FALSE FALSE TRUE FALSE TRUE
## [1] 57
## [1] 0.57
## [1] "integer"
## [1] "double"
## [1] "character"
#test functions
#is_logical, integer, double, numeric, character, atomic, list, vector
sample(10)+100
## [1] 108 109 101 107 105 110 103 106 104 102
## [1] FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE
## [1] 2 4 4 6 6 8 8 10 10 12
## Warning in 1:10 + 1:3: longer object length is not a multiple of shorter object length
## [1] 2 4 6 5 7 9 8 10 12 11
#tibble(x=1:4, y=1:2)
tibble(x=1:4, y=rep(1:2, 2))
## # A tibble: 4 x 2
## x y
## <int> <int>
## 1 1 1
## 2 2 2
## 3 3 1
## 4 4 2
tibble(x=1:4, y=rep(1:2, each=2))
## # A tibble: 4 x 2
## x y
## <int> <int>
## 1 1 1
## 2 2 1
## 3 3 2
## 4 4 2
#name vectors
c(x=1, y=2, z=4)
## x y z
## 1 2 4
set_names(1:3, c("a", "b", "c"))
## a b c
## 1 2 3
#subset
x <- c("one", "two", "three", "four", "five")
x[c(3,2,5)]
## [1] "three" "two" "five"
## [1] "one" "one" "five" "five" "five" "two"
## [1] "two" "four"
## character(0)
x <- c(10, 3, NA, 5, 8, 1, NA)
x
## [1] 10 3 NA 5 8 1 NA
## [1] 10 3 5 8 1
## [1] 10 NA 8 NA
x <- c(abc=1, def=2, xyz=5)
x[c("xyz", "def")]
## xyz def
## 5 2
#lists
x <- list(1,2,3)
x
## [[1]]
## [1] 1
##
## [[2]]
## [1] 2
##
## [[3]]
## [1] 3
## List of 3
## $ : num 1
## $ : num 2
## $ : num 3
x_named <- list(a=1, b=2, c=3)
str(x_named)
## List of 3
## $ a: num 1
## $ b: num 2
## $ c: num 3
y <- list("a", 1L, 1.5, TRUE)
y
## [[1]]
## [1] "a"
##
## [[2]]
## [1] 1
##
## [[3]]
## [1] 1.5
##
## [[4]]
## [1] TRUE
## List of 4
## $ : chr "a"
## $ : int 1
## $ : num 1.5
## $ : logi TRUE
z <- list(list(1,2), list(3,4))
str(z)
## List of 2
## $ :List of 2
## ..$ : num 1
## ..$ : num 2
## $ :List of 2
## ..$ : num 3
## ..$ : num 4
#subset
a <- list(a=1:3, b="a string", c=pi, d=list(-1,-5))
a
## $a
## [1] 1 2 3
##
## $b
## [1] "a string"
##
## $c
## [1] 3.141593
##
## $d
## $d[[1]]
## [1] -1
##
## $d[[2]]
## [1] -5
## List of 2
## $ a: int [1:3] 1 2 3
## $ b: chr "a string"
## List of 1
## $ d:List of 2
## ..$ : num -1
## ..$ : num -5
## chr "a"
## logi TRUE
## [1] 1 2 3
## [1] 1 2 3
## [1] 1 2 3
#attributes
x <- 1:10
attr(x, "greeting")
## NULL
attr(x, "greeting") <- "Hi!"
attr(x, "farewell") <- "BYE!"
attributes(x)
## $greeting
## [1] "Hi!"
##
## $farewell
## [1] "BYE!"
#augmented vectors
#factors
x <- factor(c("ab", "cd", "ab"), levels = c("ab", "cd", "ef"))
x
## [1] ab cd ab
## Levels: ab cd ef
## [1] "integer"
## $levels
## [1] "ab" "cd" "ef"
##
## $class
## [1] "factor"
#dates and date-times; since 1 January 1970
x <- as.Date("1971-01-01")
unclass(x)
## [1] 365
## [1] "double"
## $class
## [1] "Date"
x <- lubridate::ymd_hm("1970-01-01 01:00")
x
## [1] "1970-01-01 01:00:00 UTC"
## [1] 3600
## attr(,"tzone")
## [1] "UTC"
## [1] "double"
## $class
## [1] "POSIXct" "POSIXt"
##
## $tzone
## [1] "UTC"
#tibbles
tb <- tibble::tibble(x=1:5, y=5:1)
tb
## # A tibble: 5 x 2
## x y
## <int> <int>
## 1 1 5
## 2 2 4
## 3 3 3
## 4 4 2
## 5 5 1
## [1] "list"
## $names
## [1] "x" "y"
##
## $row.names
## [1] 1 2 3 4 5
##
## $class
## [1] "tbl_df" "tbl" "data.frame"
df <- data.frame(x=1:5, y=5:1)
df
## x y
## 1 1 5
## 2 2 4
## 3 3 3
## 4 4 2
## 5 5 1
## [1] "list"
## $names
## [1] "x" "y"
##
## $class
## [1] "data.frame"
##
## $row.names
## [1] 1 2 3 4 5