Vectors are the most important family of data types in base R
Vectors come in two (delicious) flavours:
NULL
? - Not a vector (but closely related - serves role of generic zero length vector, but we will get to that)3.2 Atomic Vectors
3.3 Attributes
3.4 S3 Atomic Vectors
3.5 Lists
3.6 Data Frames and Tibbles
3.7 NULL
lgl_var <- c(TRUE, FALSE)int_var <- c(1L, 6L, 10L)dbl_var <- c(1, 2.5, 4.5)chr_var <- c('these are', "some strings")
typeof()
to determine type... of.is.*()
- for example, is.integer()
c(TRUE)
## [1] TRUE
c(TRUE, 42L)
## [1] 1 42
c(TRUE, 42L, 3.14)
## [1] 1.00 42.00 3.14
c(TRUE, 42L, 3.14, "elephant")
## [1] "TRUE" "42" "3.14" "elephant"
NULL
NULL
)NA
NA
indicated element of vector is absentNULL
is equivalent R's NA
attr()
, thusly:a <- 1:3attr(a, "x") <- "abcdef"attr(a, "x")
## [1] "abcdef"
attributes()
/structure()
, respectively:a <- structure( 1:3, x = "abcdef", y = "why?")attributes(a)
## $x## [1] "abcdef"## ## $y## [1] "why?"
a
defined in the last slide..attributes(a)
## $x## [1] "abcdef"## ## $y## [1] "why?"
attributes(a[1])
## NULL
attributes(sum(a))
## NULL
# When creating it: x <- c(a = 1, b = 2, c = 3)# By assigning a character vector to names()x <- 1:3names(x) <- c("a", "b", "c")# Inline, with setNames():x <- setNames(1:3, c("a", "b", "c"))
dim
attribute to a vector allows it to behave like a 2-dimensional matrix or a multi-dimensional array.# Two scalar arguments specify row and column sizesa <- matrix(1:6, nrow = 2, ncol = 3)dim(a)
## [1] 2 3
b <- array(1:12, c(2, 3, 2))dim(b)
## [1] 2 3 2
c <- 1:6dim(c) <- c(3,2)
dim
attribute set is often thought of as 1-dimensional, but actually has NULL
dimensions.class
= "factor" and levels
which define allowed values.x <- factor(c("a", "b", "b", "a"))x
## [1] a b b a## Levels: a b
typeof(x)
## [1] "integer"
attributes(x)
## $levels## [1] "a" "b"## ## $class## [1] "factor"
grade <- ordered(c("b", "b", "a", "c"), levels = c("c", "b", "a"))grade
## [1] b b a c## Levels: c < b < a
class
= "Date". No other attributes.the_day_this_slide_was_rendered <- Sys.Date()the_day_this_slide_was_rendered
## [1] "2020-08-20"
typeof(the_day_this_slide_was_rendered)
## [1] "double"
attributes(the_day_this_slide_was_rendered)
## $class## [1] "Date"
unclass(the_day_this_slide_was_rendered) # Days since 1970-01-01
## [1] 18494
then_ct <- as.POSIXct("2018-08-01 22:00", tz = "UTC")then_ct
## [1] "2018-08-01 22:00:00 UTC"
typeof(then_ct) # Let's not forget, it was built on a double vector
## [1] "double"
attributes(then_ct)
## $class## [1] "POSIXct" "POSIXt" ## ## $tzone## [1] "UTC"
units
attribute to determine how integer should be interpretedone_week_1 <- as.difftime(1, units = "weeks")one_week_1
## Time difference of 1 weeks
attributes(one_week_1)
## $class## [1] "difftime"## ## $units## [1] "weeks"
one_week_2 <- as.difftime(7, units = "days")one_week_2
## Time difference of 7 days
attributes(one_week_2)
## $class## [1] "difftime"## ## $units## [1] "days"
lobstr::obj_size(mtcars)
## 7,208 B
l2 <- list(mtcars, mtcars, mtcars, mtcars)lobstr::obj_size(l2)
## 7,288 B
l3 <- list(list(list(1)))
l4 <- list(list(1, 2), c(3, 4))str(l4)
## List of 2## $ :List of 2## ..$ : num 1## ..$ : num 2## $ : num [1:2] 3 4
l5 <- c(list(1, 2), c(3, 4)) # If given a combination of atomic vector and list, c() will coerce vectors to lists before comibining themstr(l5) #NB, it's a list, even though we called c()
## List of 4## $ : num 1## $ : num 2## $ : num 3## $ : num 4
l6 <- c(c(1, 2), c(3, 4))str(l6) # Still an atomic vector...
## num [1:4] 1 2 3 4
typeof()
list is list
.is.list()
- test for listas.list()
df1 <- data.frame(x = 1:3, y = letters[1:3])attributes(df1)
## $names## [1] "x" "y"## ## $class## [1] "data.frame"## ## $row.names## [1] 1 2 3
df2 <- tibble(x = 1:3, y = letters[1:3]) # still a list of vectorsattributes(df2)
## $names## [1] "x" "y"## ## $row.names## [1] 1 2 3## ## $class## [1] "tbl_df" "tbl" "data.frame"
names(data.frame(`1` = 1))
## [1] "X1"
names(tibble(`1` = 1))
## [1] "1"
[
always returns tibble & $
doesn't do partial matchingI()
:df <- data.frame(x = 1:3)df$y <- list(1:2, 1:3, 1:4)data.frame( x = 1:3, y = I(list(1:2, 1:3, 1:4)))
## x y## 1 1 1, 2## 2 2 1, 2, 3## 3 3 1, 2, 3, 4
tibble( x = 1:3, y = list(1:2, 1:3, 1:4))
## # A tibble: 3 x 2## x y ## <int> <list> ## 1 1 <int [2]>## 2 2 <int [3]>## 3 3 <int [4]>
Vectors are the most important family of data types in base R
Vectors come in two (delicious) flavours:
NULL
? - Not a vector (but closely related - serves role of generic zero length vector, but we will get to that)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 |