## ----child = "../setup.Rmd"------------------------------------ ## ----setup, include=FALSE-------------------------------------- # R options options( htmltools.dir.version = FALSE, dplyr.print_min = 6, dplyr.print_max = 6, tibble.width = 65, width = 65 ) # figure height, width, dpi knitr::opts_chunk$set(echo = TRUE, fig.width = 8, fig.asp = 0.618, out.width = "60%", fig.align = "center", dpi = 300, message = FALSE) # ggplot2 ggplot2::theme_set(ggplot2::theme_gray(base_size = 16)) # set seed set.seed(1234) # fontawesome htmltools::tagList(rmarkdown::html_dependency_font_awesome()) # magick dev.off <- function(){ invisible(grDevices::dev.off()) } # conflicted library(conflicted) conflict_prefer("filter", "dplyr") # xaringanExtra library(xaringanExtra) xaringanExtra::use_panelset() # output number of lines hook_output <- knitr::knit_hooks$get("output") knitr::knit_hooks$set(output = function(x, options) { lines <- options$output.lines if (is.null(lines)) { return(hook_output(x, options)) # pass to default hook } x <- unlist(strsplit(x, "\n")) more <- "..." if (length(lines)==1) { # first n lines if (length(x) > lines) { # truncate the output, but add .... x <- c(head(x, lines), more) } } else { x <- c(more, x[lines], more) } # paste these lines together x <- paste(c(x, ""), collapse = "\n") hook_output(x, options) }) ## ----packages, echo=FALSE, message=FALSE, warning=FALSE-------- library(tidyverse) library(DT) library(scales) ## ----echo=FALSE, caption="The hierarchy of R’s vector types", out.width="120%"---- knitr::include_graphics("img/data-structures-overview.png") ## -------------------------------------------------------------- typeof(TRUE) ## -------------------------------------------------------------- mode(TRUE) ## -------------------------------------------------------------- typeof("hello") ## -------------------------------------------------------------- mode("hello") ## -------------------------------------------------------------- typeof(1.335) typeof(7) ## -------------------------------------------------------------- mode(1.335) ; mode(7) ## -------------------------------------------------------------- typeof(7L) typeof(1:3) ## -------------------------------------------------------------- mode(7L) ; mode(1:3) ## -------------------------------------------------------------- c(1, 2, 3) c("Hello", "World!") c(c("hi", "hello"), c("bye", "goodbye")) ## -------------------------------------------------------------- x <- c(TRUE, FALSE) x typeof(x) ## -------------------------------------------------------------- y <- as.numeric(x) y typeof(y) ## -------------------------------------------------------------- x <- 1:3 x typeof(x) ## -------------------------------------------------------------- y <- as.character(x) y typeof(y) ## -------------------------------------------------------------- c(1, "Hello") ; typeof(c(1, "Hello")) c(FALSE, 3L) ; typeof(c(FALSE, 3L)) ## -------------------------------------------------------------- c(1.2, 3L) ; typeof(c(1.2, 3L)) c(2L, "two") ; typeof(c(2L, "two")) ## -------------------------------------------------------------- pi / 0 0 / 0 ## -------------------------------------------------------------- 1/0 - 1/0 1/0 + 1/0 ## -------------------------------------------------------------- x <- c(1, 2, 3, 4, NA) ## -------------------------------------------------------------- mean(x) mean(x, na.rm = TRUE) summary(x) ## -------------------------------------------------------------- typeof(NA) ## -------------------------------------------------------------- # TRUE or NA TRUE | NA ## -------------------------------------------------------------- # FALSE or NA FALSE | NA ## -------------------------------------------------------------- TRUE | TRUE # if NA was TRUE TRUE | FALSE # if NA was FALSE ## -------------------------------------------------------------- FALSE | TRUE # if NA was TRUE FALSE | FALSE # if NA was FALSE ## ----message=FALSE--------------------------------------------- cat_lovers <- read_csv("data/cat-lovers.csv") ## ----echo=FALSE------------------------------------------------ cat_lovers ## -------------------------------------------------------------- cat_lovers %>% summarise(mean_cats = mean(number_of_cats)) ## ----eval=FALSE------------------------------------------------ # ?mean ## ----echo=FALSE, caption="Help for mean", out.width="75%"------ knitr::include_graphics("img/mean-help.png") ## -------------------------------------------------------------- cat_lovers %>% summarise(mean_cats = mean(number_of_cats, na.rm = TRUE)) ## -------------------------------------------------------------- glimpse(cat_lovers) ## ----echo=FALSE------------------------------------------------ options(htmltools.preserve.raw = FALSE) cat_lovers %>% datatable(options=list(scrollY=F, pageLength = 10)) ## -------------------------------------------------------------- cat_lovers %>% mutate(number_of_cats = case_when( name == "Ginger Clark" ~ 2, name == "Doug Bass" ~ 3, TRUE ~ as.numeric(number_of_cats) )) %>% summarise(mean_cats = mean(number_of_cats)) ## -------------------------------------------------------------- cat_lovers %>% mutate( number_of_cats = case_when( name == "Ginger Clark" ~ "2", name == "Doug Bass" ~ "3", TRUE ~ number_of_cats ), number_of_cats = as.numeric(number_of_cats) ) %>% summarise(mean_cats = mean(number_of_cats)) ## -------------------------------------------------------------- cat_lovers <- cat_lovers %>% #<< mutate( number_of_cats = case_when( name == "Ginger Clark" ~ "2", name == "Doug Bass" ~ "3", TRUE ~ number_of_cats ), number_of_cats = as.numeric(number_of_cats) ) ## -------------------------------------------------------------- typeof(1) ## -------------------------------------------------------------- typeof("a") ## -------------------------------------------------------------- typeof(c(1, "a")) ## -------------------------------------------------------------- temp <- c(0.2, 0.5, 1.47, 3.82, 0.2, 0.78) temp # Testing vector type is.atomic(temp) ; is.list(temp) # Testing length and value type length(temp) ; typeof(temp) ## -------------------------------------------------------------- # Two scalar arguments specify row and column sizes x <- matrix(1:6, nrow = 2, ncol = 3) x dim(x) attributes(x) # You can also modify an object in place by setting dim() z <- 1:6 dim(z) <- c(2, 3) ## -------------------------------------------------------------- # One vector argument to describe all dimensions y <- array(1:12, c(2, 3, 2)) y attributes(y) ## -------------------------------------------------------------- x <- factor(c("BS", "MS", "PhD", "MS")) x attributes(x) ## -------------------------------------------------------------- glimpse(x) as.integer(x) ## -------------------------------------------------------------- y <- as.Date("2020-01-01") y attributes(y) typeof(y) class(y) ## -------------------------------------------------------------- as.integer(y) as.integer(y) / 365 # roughly 50 yrs ## -------------------------------------------------------------- l <- list( x = 1:4, y = c("hi", "hello", "bye"), z = c(TRUE, FALSE) ) l ## -------------------------------------------------------------- length(l) typeof(l) attributes(l) ## -------------------------------------------------------------- df <- data.frame(x = 1:2, y = c("hi", "hello")) df attributes(df) ## -------------------------------------------------------------- str(df) str(l) ## -------------------------------------------------------------- str(temp) str(z) str(y) str(x) ## -------------------------------------------------------------- temp[2] ; temp[2:3] z[,1] ; z[1,2] l[1] ; l[[1]] ; l$x ## -------------------------------------------------------------- df df[,1] ; df$x ## -------------------------------------------------------------- df %>% pull(y)