Section 3.2: atomic vectors
Section 3.3: attributes
Section 3.4: "special" vectors (S3 atomic vectors)
Section 3.5: lists
Section 3.6: data frames and tibbles
Section 3.7: NULL
Section 3.2: atomic vectors
Section 3.3: attributes
Section 3.4: "special" vectors (S3 atomic vectors)
Section 3.5: lists
Section 3.6: data frames and tibbles
Section 3.7: NULL
atomic
list
atomic
list
... and there is also NULL
c(TRUE, FALSE, T, F)c(1234L, 42L)c(3.14, .314e1, 0xbada55)c('single quote', "double quote")
c(TRUE, FALSE, T, F)c(1234L, 42L)c(3.14, .314e1, 0xbada55)c('single quote', "double quote")
... also raw and complex
raw(42)complex(real = 0, imaginary = -1)
c(TRUE, FALSE, T, F)c(1234L, 42L)c(3.14, .314e1, 0xbada55)c('single quote', "double quote")
... also raw and complex
raw(42)complex(real = 0, imaginary = -1)
typeof()
c(1, 1.01) # to double
## [1] 1.00 1.01
c(1, '1') # to character
## [1] "1" "1"
c(1, TRUE) # to integer
## [1] 1 1
c(1, 1.01) # to double
## [1] 1.00 1.01
c(1, '1') # to character
## [1] "1" "1"
c(1, TRUE) # to integer
## [1] 1 1
as.*()
functionsas.integer(c(1, 1.01))
## [1] 1 1
c(1, 1.01) # to double
## [1] 1.00 1.01
c(1, '1') # to character
## [1] "1" "1"
c(1, TRUE) # to integer
## [1] 1 1
as.*()
functionsas.integer(c(1, 1.01))
## [1] 1 1
NA
as.integer(c('1', '1.01', 'a'))
## Warning: NAs introduced by coercion
## [1] 1 1 NA
NA
and NULL
NA
and NULL
NA
is a "sentinel" value for explicit missingness
NA
can be of any type, e.g. NA_integer_
, NA_character_
, etc.
NA
s usually result in more NA
s1 + NA
## [1] NA
...although not always
1 | NA
## [1] TRUE
is.na()
NA
and NULL
NA
is a "sentinel" value for explicit missingness
NA
can be of any type, e.g. NA_integer_
, NA_character_
, etc.
NA
s usually result in more NA
s1 + NA
## [1] NA
...although not always
1 | NA
## [1] TRUE
is.na()
NULL
is its own vector typetypeof(NULL)
## [1] "NULL"
length(NULL)
## [1] 0
x <- NULLattr(x, 'y') <- 1 # error
is.null()
Name-value pairs of metadata for R objects
Get and set a single attribute with attr()
x <- 'a'attr(x, 'what') <- 'apple'attr(x, 'what')
## [1] "apple"
Name-value pairs of metadata for R objects
Get and set a single attribute with attr()
x <- 'a'attr(x, 'what') <- 'apple'attr(x, 'what')
## [1] "apple"
attributes()
and structure()
Name-value pairs of metadata for R objects
Get and set a single attribute with attr()
x <- 'a'attr(x, 'what') <- 'apple'attr(x, 'what')
## [1] "apple"
attributes()
and structure()
x <- structure('a', what = 'apple', type = 'fruit')attributes(x)
## $what## [1] "apple"## ## $type## [1] "fruit"
Name-value pairs of metadata for R objects
Get and set a single attribute with attr()
x <- 'a'attr(x, 'what') <- 'apple'attr(x, 'what')
## [1] "apple"
attributes()
and structure()
x <- structure('a', what = 'apple', type = 'fruit')attributes(x)
## $what## [1] "apple"## ## $type## [1] "fruit"
names()
and dim()
, most attributes are lost with calculationsattributes(x[1])
## NULL
names()
names()
names()
can be assigned in multiple waysx <- c(apple = 'a', banana = 'b') # 1xy <- c('a', 'b')names(y) <- c('apple', 'banana') # 2ysetNames(y, c('apple', 'banana')) # 3
## apple banana ## "a" "b" ## apple banana ## "a" "b" ## apple banana ## "a" "b"
dim()
dim()
dim()
has the capability of turning a 1-d vector into a 2-d matrix or an n-d arraya <- matrix(1:6, nrow = 2, ncol = 3)a
## [,1] [,2] [,3]## [1,] 1 3 5## [2,] 2 4 6
b <- array(1:6, dim = c(1, 3, 2))b
## , , 1## ## [,1] [,2] [,3]## [1,] 1 2 3## ## , , 2## ## [,1] [,2] [,3]## [1,] 4 5 6
dim()
dim()
has the capability of turning a 1-d vector into a 2-d matrix or an n-d arraya <- matrix(1:6, nrow = 2, ncol = 3)a
## [,1] [,2] [,3]## [1,] 1 3 5## [2,] 2 4 6
b <- array(1:6, dim = c(1, 3, 2))b
## , , 1## ## [,1] [,2] [,3]## [1,] 1 2 3## ## , , 2## ## [,1] [,2] [,3]## [1,] 4 5 6
dim()
dim()
has the capability of turning a 1-d vector into a 2-d matrix or an n-d arraya <- matrix(1:6, nrow = 2, ncol = 3)a
## [,1] [,2] [,3]## [1,] 1 3 5## [2,] 2 4 6
b <- array(1:6, dim = c(1, 3, 2))b
## , , 1## ## [,1] [,2] [,3]## [1,] 1 2 3## ## , , 2## ## [,1] [,2] [,3]## [1,] 4 5 6
Weird things
dim
attribute has NULL
dimension dim()
dim()
has the capability of turning a 1-d vector into a 2-d matrix or an n-d arraya <- matrix(1:6, nrow = 2, ncol = 3)a
## [,1] [,2] [,3]## [1,] 1 3 5## [2,] 2 4 6
b <- array(1:6, dim = c(1, 3, 2))b
## , , 1## ## [,1] [,2] [,3]## [1,] 1 2 3## ## , , 2## ## [,1] [,2] [,3]## [1,] 4 5 6
Weird things
1-d vector without a dim
attribute has NULL
dimension
Matrices and arrays can be a single column or row vector
class
attribute, making them S3 objectsclass
attribute, making them S3 objectsfactor
(categorical), Date
(Date), POSIXct
(date-time), duration
(difftime).class
and levels
Has two attributes: class
and levels
Built on top of integers, not characters
fruits <- factor(c('banana', 'apple', 'carrot'))fruits
## [1] banana apple carrot## Levels: apple banana carrot
Has two attributes: class
and levels
Built on top of integers, not characters
fruits <- factor(c('banana', 'apple', 'carrot'))fruits
## [1] banana apple carrot## Levels: apple banana carrot
x <- ordered(c('two', 'three', 'one'), levels = c('one', 'two', 'three'))x
## [1] two three one ## Levels: one < two < three
Date
, POSIXct
, and duration
Date
, POSIXct
, and duration
Date
, POSIXct
, and duration
All built on top of doubles
Dates have class = "Date"
Date
, POSIXct
, and duration
All built on top of doubles
Dates have class = "Date"
Date-times are trickier...
Date
, POSIXct
, and duration
All built on top of doubles
Dates have class = "Date"
Date-times are trickier...
Date
, POSIXct
, and duration
All built on top of doubles
Dates have class = "Date"
Date-times are trickier...
Represent seconds since Jan. 1, 1970
POSIXct
isn't the only possible class; there's also POSIXlt
Date
, POSIXct
, and duration
All built on top of doubles
Dates have class = "Date"
Date-times are trickier...
Represent seconds since Jan. 1, 1970
POSIXct
isn't the only possible class; there's also POSIXlt
Also have a "parent" class of POSIXt
Date
, POSIXct
, and duration
All built on top of doubles
Dates have class = "Date"
Date-times are trickier...
Represent seconds since Jan. 1, 1970
POSIXct
isn't the only possible class; there's also POSIXlt
Also have a "parent" class of POSIXt
Have a tzone
attribute
Date
, POSIXct
, and duration
All built on top of doubles
Dates have class = "Date"
Date-times are trickier...
Represent seconds since Jan. 1, 1970
POSIXct
isn't the only possible class; there's also POSIXlt
Also have a "parent" class of POSIXt
Have a tzone
attribute
class = "difftime"
and units
corresponding to a temporal unit, e.g. "day"
x <- 1Llobstr::obj_size(x)
## 56 B
lobstr::obj_size(rep(x, 3L))
## 64 B
x <- 1Llobstr::obj_size(x)
## 56 B
lobstr::obj_size(rep(x, 3L))
## 64 B
c
is different than wrapping with list()
x <- list(a = 1, b = 2)y <- list(c = -1, d = -2)length(list(x, y))
## [1] 2
length(c(x, y))
## [1] 4
x <- 1Llobstr::obj_size(x)
## 56 B
lobstr::obj_size(rep(x, 3L))
## 64 B
c
is different than wrapping with list()
x <- list(a = 1, b = 2)y <- list(c = -1, d = -2)length(list(x, y))
## [1] 2
length(c(x, y))
## [1] 4
df <- data.frame(col1 = 1:2, col2 = c('a', 'b'))df
## col1 col2## 1 1 a## 2 2 b
df <- data.frame(col1 = 1:2, col2 = c('a', 'b'))df
## col1 col2## 1 1 a## 2 2 b
class(df$col2)
## [1] "character"
df <- data.frame(col1 = 1:2, col2 = c('a', 'b'))df
## col1 col2## 1 1 a## 2 2 b
class(df$col2)
## [1] "character"
... which spawned tibbles (with the {tibble}
package)
tbl <- tibble::tibble(col1 = 1:2, col2 = c('a', 'b'))class(tbl$col2)
## [1] "character"
Tibble don't coerce strings to factors by default
Tibbles discourage rownames, which are generally "bad"
Tibble don't coerce strings to factors by default
Tibbles discourage rownames, which are generally "bad"
Tibble don't coerce strings to factors by default
Tibbles discourage rownames, which are generally "bad"
Tibbles have a "prettier" print method
Tibbles have stricter subsetting rules
data.frame(x = 1:2, y = I(list(1:3, 1:4)))
## x y## 1 1 1, 2, 3## 2 2 1, 2, 3, 4
data.frame(x = 1:2, y = I(list(1:3, 1:4)))
## x y## 1 1 1, 2, 3## 2 2 1, 2, 3, 4
tibble::tibble(x = 1:2, y = list(1:3, 1:4))
## # A tibble: 2 x 2## x y ## <int> <list> ## 1 1 <int [3]>## 2 2 <int [4]>
data.frame(x = 1:2, y = I(list(1:3, 1:4)))
## x y## 1 1 1, 2, 3## 2 2 1, 2, 3, 4
tibble::tibble(x = 1:2, y = list(1:3, 1:4))
## # A tibble: 2 x 2## x y ## <int> <list> ## 1 1 <int [3]>## 2 2 <int [4]>
data.frame(x = 1:2, y = matrix(3:6, nrow = 2))data.frame(x = 1:2, y = data.frame(a = 3:4, b = 5:6))
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |