diff --git a/week1/slides.R b/week1/slides.R new file mode 100644 index 0000000..c767113 --- /dev/null +++ b/week1/slides.R @@ -0,0 +1,507 @@ +## Ex 1 ------------------------------------------------------------------------ +df <- data.frame(runif(3), runif(3)) +names(df) <- c(1, 2) + + +## Ex 2 ------------------------------------------------------------------------ +x <- runif(1e6) +y <- list(x, x, x) + + +## Ex 3 ------------------------------------------------------------------------ +a <- c(1, 5, 3, 2) +b <- a +b[[1]] <- 10 + + +## Binding basics + +x <- c(1, 2, 3) +y <- x +library(lobstr) +obj_addr(x) +obj_addr(y) + + +## Syntactic names +_abc <- 1 +if <- 10 + +`_abc` <- 1 +`_abc` + + +## Copy on modify +x <- c(1, 2, 3) +y <- x + +y[[3]] <- 4 +x + +x <- c(1, 2, 3) +tracemem(x) + +y <- x +y[[3]] <- 4L + + +y[[3]] <- 5L +untracemem(x) + + +## ------------------------------------------------------------------------ +v <- c(1, 2, 3) + + +## ----echo = FALSE, out.width = NULL-------------------------------------- +knitr::include_graphics("../diagrams/name-value/v-inplace-1.png") + + +## ------------------------------------------------------------------------ +v[[3]] <- 4 + + +## ----echo = FALSE, out.width = NULL-------------------------------------- +knitr::include_graphics("../diagrams/name-value/v-inplace-2.png") + + +## ------------------------------------------------------------------------ +f <- function(a) { + a +} + +x <- c(1, 2, 3) +tracemem(x) +z <- f(x) +# there's no copy here! +untracemem(x) + + +## ----list1--------------------------------------------------------------- +l1 <- list(1, 2, 3) + + +## ----list2, dependson = "list1"------------------------------------------ +l2 <- l1 + + +## ----list3, dependson = "list2"------------------------------------------ +l2[[3]] <- 4 + + +## ------------------------------------------------------------------------ +d1 <- data.frame(x = c(1, 5, 6), y = c(2, 4, 3)) + + +## ------------------------------------------------------------------------ +d2 <- d1 +d2[, 2] <- d2[, 2] * 2 + + +## ------------------------------------------------------------------------ +d1 <- data.frame(x = c(1, 5, 6), y = c(2, 4, 3)) + + +## ------------------------------------------------------------------------ +d3 <- d1 +d3[1, ] <- d3[1, ] * 3 + + +## ------------------------------------------------------------------------ +x <- c("a", "a", "abc", "d") + +## ----echo = FALSE, out.width = NULL-------------------------------------- +knitr::include_graphics("../diagrams/name-value/character.png") + + +## ----echo = FALSE, out.width = NULL-------------------------------------- +knitr::include_graphics("../diagrams/name-value/character-2.png") + + +## ------------------------------------------------------------------------ +a <- 1:10 +b <- list(a, a) +c <- list(b, a, 1:10) + + +## ------------------------------------------------------------------------ +x <- list(1:10) +x[[2]] <- x + + +## ------------------------------------------------------------------------ +obj_size(ggplot2::diamonds) +banana <- "bananas bananas bananas" +obj_size(banana) +obj_size(rep(banana, 100)) + + +## ------------------------------------------------------------------------ +x <- runif(1e6) +obj_size(x) +y <- list(x, x, x) +obj_size(y) +obj_size(x, y) + + +## ------------------------------------------------------------------------ +obj_size(1:3) +obj_size(1:1e6) +obj_size(c(1:1e6, 10)) +obj_size(2 * (1:1e6)) + + +## ------------------------------------------------------------------------ +#| eval: false +## a <- runif(1e6) +## obj_size(a) +## +## b <- list(a, a) +## obj_size(b) +## obj_size(a, b) +## +## b[[1]][[1]] <- 10 +## obj_size(b) +## obj_size(a, b) +## +## b[[2]][[1]] <- 10 +## obj_size(b) +## obj_size(a, b) + + +## ------------------------------------------------------------------------ +#| output: false +x <- data.frame(matrix(runif(3 * 1e4), ncol = 3)) +medians <- vapply(x, median, numeric(1)) +tracemem(x) + + +## ------------------------------------------------------------------------ +for (i in seq_along(medians)) { + x[[i]] <- x[[i]] - medians[[i]] +} + + +## ------------------------------------------------------------------------ +#| output: false +y <- as.list(x) +tracemem(y) + + +## ------------------------------------------------------------------------ +for (i in 1:3) { + y[[i]] <- y[[i]] - medians[[i]] +} + + +## ------------------------------------------------------------------------ +x <- 1:3 + + +## ------------------------------------------------------------------------ +x <- 2:4 + + +## ------------------------------------------------------------------------ +rm(x) + + +## ------------------------------------------------------------------------ +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") + + +## ------------------------------------------------------------------------ +c(c(1, 2), c(3, 4)) + + +## ------------------------------------------------------------------------ +typeof(lgl_var) +typeof(int_var) +typeof(dbl_var) +typeof(chr_var) + + +## ------------------------------------------------------------------------ +NA > 5 +10 * NA +!NA + + +## ------------------------------------------------------------------------ +NA ^ 0 +NA | TRUE +NA & FALSE + + +## ------------------------------------------------------------------------ +x <- c(NA, 5, NA, 10) +x == NA +is.na(x) + + +## ------------------------------------------------------------------------ +str(c("a", 1)) +x <- c(FALSE, FALSE, TRUE) +as.numeric(x) +sum(x) +as.integer(c("1", "1.5", "a")) + + +## ----eval=FALSE---------------------------------------------------------- +## c(1, FALSE) +## c("a", 1) +## c(TRUE, 1L) + + +## ------------------------------------------------------------------------ +a <- 1:3 +attr(a, "x") <- "abcdef" +a + + +## ------------------------------------------------------------------------ +attr(a, "y") <- 4:6 +str(attributes(a)) + +# Or equivalently +a <- structure( + 1:3, + x = "abcdef", + y = 4:6 +) +str(attributes(a)) + + +## ------------------------------------------------------------------------ +# When creating it: +x <- c(a = 1, b = 2, c = 3) + +# By assigning a character vector to names() +x <- 1:3 +names(x) <- c("a", "b", "c") + +# Inline, with setNames(): +x <- setNames(1:3, c("a", "b", "c")) + + +## ------------------------------------------------------------------------ +x + + +## ------------------------------------------------------------------------ +# Two scalar arguments specify row and column sizes +x <- matrix(1:6, nrow = 2, ncol = 3) +x + + +## ------------------------------------------------------------------------ +# One vector argument to describe all dimensions +y <- array(1:12, c(2, 3, 2)) +y + + +## ------------------------------------------------------------------------ +# You can also modify an object in place by setting dim() +z <- 1:6 +dim(z) <- c(3, 2) +z + + +## ------------------------------------------------------------------------ +str(1:3) # 1d vector +str(matrix(1:3, ncol = 1)) # column vector +str(matrix(1:3, nrow = 1)) # row vector +str(array(1:3, 3)) # "array" vector + + +## ------------------------------------------------------------------------ +x1 <- array(1:5, c(1, 1, 5)) +x2 <- array(1:5, c(1, 5, 1)) +x3 <- array(1:5, c(5, 1, 1)) + + +## ------------------------------------------------------------------------ +x <- factor(c("a", "b", "b", "a")) +x + + +## ------------------------------------------------------------------------ +typeof(x) +attributes(x) + + +## ------------------------------------------------------------------------ +sex_char <- c("m", "m", "m") +sex_factor <- factor(sex_char, levels = c("m", "f")) + +table(sex_char) +table(sex_factor) + + +## ------------------------------------------------------------------------ +grade <- ordered(c("b", "b", "a", "c"), levels = c("c", "b", "a")) +grade + + +## ------------------------------------------------------------------------ +today <- Sys.Date() + +typeof(today) +attributes(today) + + +## ------------------------------------------------------------------------ +date <- as.Date("1970-02-01") +unclass(date) + + +## ------------------------------------------------------------------------ +now_ct <- as.POSIXct("2018-08-01 22:00", tz = "UTC") +now_ct + +typeof(now_ct) +attributes(now_ct) + + +## ------------------------------------------------------------------------ +structure(now_ct, tzone = "Asia/Tokyo") +structure(now_ct, tzone = "America/New_York") +structure(now_ct, tzone = "Australia/Lord_Howe") + + +## ----results = FALSE----------------------------------------------------- +f1 <- factor(letters) +levels(f1) <- rev(levels(f1)) + + +## ----results = FALSE----------------------------------------------------- +f2 <- rev(factor(letters)) +f3 <- factor(letters, levels = rev(letters)) + + +## ------------------------------------------------------------------------ +l1 <- list( + 1:3, "a", c(TRUE, FALSE, TRUE), c(2.3, 5.9) +) +typeof(l1) +str(l1) + + +## ------------------------------------------------------------------------ +l3 <- list(list(list(1))) +str(l3) + + +## ------------------------------------------------------------------------ +l4 <- list(list(1, 2), c(3, 4)) +l5 <- c(list(1, 2), c(3, 4)) +str(l4) +str(l5) + + +## ------------------------------------------------------------------------ +list(1:3) +as.list(1:3) + + +## ------------------------------------------------------------------------ +df1 <- data.frame(x = 1:3, y = letters[1:3]) +typeof(df1) +attributes(df1) + + +## ------------------------------------------------------------------------ +library(tibble) +df2 <- tibble(x = 1:3, y = letters[1:3]) +typeof(df2) +attributes(df2) + + +## ------------------------------------------------------------------------ +names(data.frame(`1` = 1)) + +names(tibble(`1` = 1)) + + +## ----error = TRUE-------------------------------------------------------- +data.frame(x = 1:4, y = 1:2) +tibble(x = 1:4, y = 1:2) + + +## ------------------------------------------------------------------------ +tibble( + x = 1:3, + y = x * 2, + z = 5 +) + + +## ------------------------------------------------------------------------ +df3 <- data.frame( + age = c(35, 27, 18), + hair = c("blond", "brown", "black"), + row.names = c("Bob", "Susan", "Sam") +) +df3 + + +## ------------------------------------------------------------------------ +as_tibble(df3, rownames = "name") + + +## ------------------------------------------------------------------------ +dplyr::starwars + + +## ------------------------------------------------------------------------ +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)) +) + + +## ------------------------------------------------------------------------ +tibble( + x = 1:3, + y = list(1:2, 1:3, 1:4) +) + + +## ------------------------------------------------------------------------ +dfm <- tibble( + x = 1:3 * 10, + y = matrix(1:9, nrow = 3), + z = data.frame(a = 3:1, b = letters[1:3]) +) +str(dfm) + + +## ----error = TRUE-------------------------------------------------------- +length(NULL) + + +## ------------------------------------------------------------------------ +x <- NULL +x == NULL +is.null(x) + + +## ----eval = FALSE-------------------------------------------------------- +## mtcars[mtcars$cyl = 4, ] +## mtcars[-1:4, ] +## mtcars[mtcars$cyl <= 5] +## mtcars[mtcars$cyl == 4 | 6, ] + + +## ------------------------------------------------------------------------ +#| eval: false +## mod <- lm(mpg ~ wt, data = mtcars) +