class: middle, center, title-slide .title[ # Introduction to R ] .subtitle[ ## Data Visualization ] .author[ ### Johan Larsson ] .author[ ### Behnaz Pirzamanbein ] .institute[ ### The Department of Statistics, Lund University ] --- ## R .pull-left[ * a programming language **and** piece of software * designed for **statistical** applications * stable * extensive ecosystem of packages * **excellent** facilities for visualization ] .pull-right[ <img src="images/r-logo.png" width="70%" style="display: block; margin: auto;" /> ] ### Code in R ```r x <- c(1, 2) mean(x) ``` ``` ## [1] 1.5 ``` --- ## R Studio .pull-left[ * graphical user interface for R * combines text editor, console, and plot window * useful for organizing projects * for this course, we **assume** that you will be using R studio ] .pull-right[ <img src="images/rstudio.png" width="90%" style="display: block; margin: auto;" /> ] --- background-image: url("images/rstudio-screenshot.png") background-size: contain --- background-image: url("images/rstudio-screenshot-editor.png") background-size: contain --- background-image: url("images/rstudio-screenshot-console.png") background-size: contain --- background-image: url("images/rstudio-screenshot-plot.png") background-size: contain --- ## Everything in R is an Object Use the `<-` operator to assign values from the right-hand to the left-hand side. ```r x <- c(1, 9) x ``` ``` ## [1] 1 9 ``` Objects can be copied or modified in place. ```r # copy the contents of x to create a new variable y y <- x # replace the first element of y y[1] <- 10 y ``` ``` ## [1] 10 9 ``` --- ## Data Classes in R Data classes in R represents the type of storage. * **numeric**: quantitative data * **logical**: yes or no * **character**: nominal data ```r x <- c(0.1, 0.2) # numeric y <- c(TRUE, FALSE) # logical z <- c("foo", "bar") # character ``` -- Other types of classes, such as **integer**, **double**, and **factor**, exist but are not (very) important for us. --- ## Data Structures in R Objects of the various **data classes** may be stored into **data structures**, such as - **vector**: one-dimensional list, - **matrix**: two-dimensional table, - elements must have **same** data class - **data.frame**: two-dimensional table, - elements can have **different** data classes - **list**: list of items (data frames, matrices, vectors) of potentially different lengths. --- class: center, middle <img src="images/data-structures.svg" width="90%" style="display: block; margin: auto;" /> --- ### Vectors All elements of vectors must be of the same type. It can be constructed using `c()`. ```r c(1.3, 4, 5) # numeric ``` ``` ## [1] 1.3 4.0 5.0 ``` ```r c(TRUE, FALSE, TRUE) # logical ``` ``` ## [1] TRUE FALSE TRUE ``` ```r c("apple", "pear") # character ``` ``` ## [1] "apple" "pear" ``` ```r c(1, "3", TRUE) # mix (converted to character) ``` ``` ## [1] "1" "3" "TRUE" ``` --- ### Accessing Elements in Vectors Access elements in vectors with `[` operator. ```r x <- c("a", "b", "c") x[1] # access first element ``` ``` ## [1] "a" ``` -- Access several elements of vectors by indexing with vector of integers. ```r x[c(1, 2)] # or x[1:2] ``` ``` ## [1] "a" "b" ``` --- ### Matrices Create matrices with `matrix()`, `cbind()`, and `rbind()`. ```r A <- cbind(c(1, 2), c(2, 3)) # numeric matrix A ``` ``` ## [,1] [,2] ## [1,] 1 2 ## [2,] 2 3 ``` -- Access elements of matrices, rows, and columns using `[` operator. ```r A[2, 2] # lower right element ``` ``` ## [1] 3 ``` ```r A[1, ] # first row, A[, 2] accesses second column ``` ``` ## [1] 1 2 ``` --- ### Data Frames Data frames are like matrices, but columns may have different classes. ```r d <- data.frame(x = c(1, 2), y = c("apple", "pear")) d ``` ``` ## x y ## 1 1 apple ## 2 2 pear ``` -- Access elements of data frames using `[` or `$` operator. ```r d[, 1] # first column (x) ``` ``` ## [1] 1 2 ``` ```r d$x # same as above! ``` ``` ## [1] 1 2 ``` --- ### Lists Lits are a collection of objects of **any kind** (of any dimensions). ```r # <name> = <element> syntax x <- list(a = c("foo", "bar"), b = matrix(1:4, 2, 2)) x ``` ``` ## $a ## [1] "foo" "bar" ## ## $b ## [,1] [,2] ## [1,] 1 3 ## [2,] 2 4 ``` -- A data frame is in fact a special type of list. --- ## Functions Everything that happens in R is the result of a function call. -- ```r my_function <- function(x, y) { x + 2*y } my_function(1, 3) ``` ``` ## [1] 7 ``` -- - `x` and `y` are the **arguments** -- - `x + 2*y` is the **body** -- - The last line of the function decides what the function returns, unless `return()` is used somewhere inside the function. --- ### Arguments Arguments can be specified either by **name** or by **location**. ```r my_function(3, 1) # by location ``` ``` ## [1] 5 ``` ```r my_function(y = 1, x = 3) # by name ``` ``` ## [1] 5 ``` -- In `my_function()`, both arguments are **mandatory**, but this is not always the case. Here we give `y` a **default** value. ```r my_other_function <- function(x, y = 3) { x + 2*y } my_other_function(1) ``` ``` ## [1] 7 ``` --- ### Functions in This Course - We won't be writing our own function in this course. -- - But it is important to understand how to * call functions, * read function documentation, and * figure out what a function's default arguments imply. --- ## Packages There are more than 16,000 packages on [CRAN](https://cran.r-project.org/web/packages/). It is easy to install! ```r install.packages("ggplot2") ``` Load the package by calling `library()`. ```r library(ggplot2) ``` -- There are even more packages available via [bioconductor](https://www.bioconductor.org) and git repositories on [github](https://github.com/) and similar sites. ```r # install.packages("remotes") remotes::install_github("hrbrmstr/waffle") ``` But in this course we will **only** use CRAN packages. --- ## Getting Help - Learn how to read object documentation, particularly for functions. * `F1` shortcut in R Studio * `help(function)` or `?function` in console - Most problems you run into have been encountered (and solved) by others. Use * the course discussion board, * [stack overflow](https://stackoverflow.com/) (tag `r`), or * a search engine.