class: center, middle, inverse, title-slide # Lecture 1 ## R/RStudio basics ### Jung-Jin Lee ### Jan 5, 2021 --- ## Help on slides At any time, press `h` to open `Help`. --- ## What is statistics? - Statistics is the science of learning from data - The goal of statistics is to summarize data in a way that allows for easy descriptions or inferences of the data - Data visualization and computing are important parts of statistics - R, a language and environment for statistical computing and graphics, will be used as a main tool throughout the course --- ## Why R? - Specialized for data analysis - Many packages have been developed and are available - Excellent support for data visualization - Free! --- ## Installing R/RStudio - To install R, go to https://www.r-project.org/ and follow instruction there - Click on CRAN under Download on the left - Choose the closest mirror site (e.g. http://lib.stat.cmu.edu/R/CRAN/) - Download R for your platform (Windows/Mac/Linux) - To install RStudio (an integrated development environment for R), go to https://www.rstudio.com/ and follow instruction - Also, RStudio Cloud is available at https://rstudio.cloud/ --- ## RStudio IDE overview <p align = "center"> <img src = rstudio.png> </p> --- ## Additional software and libraries - (Not essential) If at all possible, install `\(\TeX\)` using one of the following options: - MiKTeX (http://miktex.org): recommended for Windows users - MacTeX (http://tug.org/mactex): recommended for Mac users - Open RStudio, and go to Tools `\(\rightarrow\)` Install Packages... and install the following libraries using default options: - `tidyverse` - `tinytex` (unnecessary if you installed `\(\TeX\)`) --- ## Using R console as a calculator ```r 5 + 3 ``` ``` ## [1] 8 ``` ```r # everything after # (pound sign) is ignored # useful for comments 2 * 5 # product of 2 and 5 ``` ``` ## [1] 10 ``` ```r 3^2 ``` ``` ## [1] 9 ``` ```r 3**2 ``` ``` ## [1] 9 ``` --- ## R project setup - Make a directory that will be used for the class (e.g. C:/Users/jungl/MLAS_530S_2021) - For easy access, all the data files and scripts used in the class are assumed to be saved in this directory - File `\(\rightarrow\)` New Project `\(\rightarrow\)` Existing Directory `\(\rightarrow\)` Create Project from Existing Directory `\(\rightarrow\)` Choose C:/Users/jungl/MLAS_530S_2021 - You can start RStudio by opening the .Rproj file - Running `getwd()` in console shows the current working directory, which should be the class directory you made previously. - If you have not installed `\(\TeX\)`, run following (it may take some time) in console - this is necessary to generate pdf output: ```r tinytex::install_tinytex() ``` --- ## R scripts / R Markdown / Others - File `\(\rightarrow\)` New File `\(\rightarrow\)` R Script for a plain R script - A part of an R script is copied into the R console and run - Can be saved for future use - File `\(\rightarrow\)` New File `\(\rightarrow\)` R Markdown for an R markdown files - Mixture of plain text and R codes (called *chunks*) - Great for generating report (preferred for **homework submission!**) - Can be rendered into (HTML/PDF/Word - Useful chunk options: `message = F, echo = F, eval = F` - Inline coding can be done using a pair of backticks and `r`: ` `r 2+3`\` - R script and R markdown files can also be developed into presentation (`xaringan`), web application (`Shiny`), blog (`blogdown`), and book (`bookdown`) - Not covered in this course --- ## Inline/Console output control <p align = "center"> <img src = chunk_output_inline.png width = "400"> </p> --- ## Variables Variables can be used to store numerical values. - A variable can be a combination of letters (case sensitive), digits, period (.) and underscore (_). - It must start with a letter or a period. If it starts with a period, it cannot be followed by a digit. - Reserved words in R, such as `TRUE`, cannot be used as variables. ```r Num1 <- 5 Num2 <- 3 sum_num <- Num1 + Num2 prod.num <- Num1 * Num2 *print(sum_num) ``` ``` ## [1] 8 ``` ```r print(prod.num) ``` ``` ## [1] 15 ``` --- ## Characters Variables can also be used to store character values. ```r first_name <- "John" # use quotation marks for character strings last_name <- 'Smith' # single quotation also works print(first_name) # print() can be omitted, but encouraged ``` ``` ## [1] "John" ``` ```r print(last_name) ``` ``` ## [1] "Smith" ``` --- ## Logical values ```r TF1 <- T # upper case T, not t TF2 <- F # upper case F, not f print(TF1) ``` ``` ## [1] TRUE ``` ```r print(TF2) ``` ``` ## [1] FALSE ``` ```r TF3 <- TRUE # can use TRUE instead of T TF4 <- FALSE # can use FALSE instead of F print(TF3) ``` ``` ## [1] TRUE ``` ```r print(TF4) ``` ``` ## [1] FALSE ``` --- ## Logical values, continued ```r p <- 4; q <- 6 # semicolon (;) can be used to separate lines call1 <- p > 5 call2 <- q > 5 call3 <- q > 6 call4 <- q >= 6 print(call1) ``` ``` ## [1] FALSE ``` ```r print(call2) ``` ``` ## [1] TRUE ``` ```r print(call3) ``` ``` ## [1] FALSE ``` ```r print(call4) ``` ``` ## [1] TRUE ``` --- ## Combination of logical values ```r p <- 4; q <- 6 *print(p < 5 & q < 7) # & means "and": TRUE only if both are TRUE ``` ``` ## [1] TRUE ``` ```r print(p < 5 & q < 5) ``` ``` ## [1] FALSE ``` ```r *print(p < 5 | q < 5) # | means "or": FALSE only if both are FALSE ``` ``` ## [1] TRUE ``` ```r *print(!(p < 5)) # ! means "not" ``` ``` ## [1] FALSE ``` --- ## Functions One or more values can produce new one using R functions. ```r a <- 1; b <- 3 *s <- sum(a, b) # sum() is a function that computes the sum print(s) ``` ``` ## [1] 4 ``` ```r first_name <- "John" last_name <- "Smith" # paste() puts together multiple character strings *full_name <- paste(first_name, last_name) print(full_name) ``` ``` ## [1] "John Smith" ``` For detailed usage of a function, type `help(function_name)`, e.g. `help(paste)` or `?function_name`, e.g. `?paste`. --- ## Vectors Multiple numerical values or characters can be saved as a single variable using an R function `c()`. ```r *vec1 <- c(2, 3, 5, 7, 1, 4) print(vec1) ``` ``` ## [1] 2 3 5 7 1 4 ``` ```r vec2 <- c("Joe", "waited", "for", "the", "train") print(vec2) ``` ``` ## [1] "Joe" "waited" "for" "the" "train" ``` ```r *vec3 <- vec2 %in% c("Joe", "for") # %in% determines membership print(vec3) ``` ``` ## [1] TRUE FALSE TRUE FALSE FALSE ``` --- ## Functions applied to a vector -- part I ```r *length(vec1) # number of components of a vector ``` ``` ## [1] 6 ``` ```r sum(vec1) ``` ``` ## [1] 22 ``` -- **Exercise**: sort the numbers 2, 3, 5, 7, 1, 4 from the smallest to the largest and vice versa. -- ```r *sort(vec1) ``` ``` ## [1] 1 2 3 4 5 7 ``` -- ```r sort(vec1, decreasing = T) ``` ``` ## [1] 7 5 4 3 2 1 ``` --- ## Functions applied to a vector -- part II ```r collapse1 <- paste(vec2, collapse = " "); print(collapse1) ``` ``` ## [1] "Joe waited for the train" ``` ```r collapse2 <- paste(vec2, collapse = "_"); print(collapse2) ``` ``` ## [1] "Joe_waited_for_the_train" ``` -- **Exercise**: produce the following output ``` ## [1] "Joewaitedforthetrain" ``` -- ```r collapse3 <- paste(vec2, collapse = ""); print(collapse3) ``` ``` ## [1] "Joewaitedforthetrain" ``` --- ## Functions applied to a vector -- part III ```r print(vec1) ``` ``` ## [1] 2 3 5 7 1 4 ``` Which elements are greater than 3? -- ```r vec4 <- vec1 > 3 print(vec4) ``` ``` ## [1] FALSE FALSE TRUE TRUE FALSE TRUE ``` -- How many elements of `vec1` are greater than 3? -- ```r sum(vec4) # in R, TRUE is 1, FALSE is 0 ``` ``` ## [1] 3 ``` --- ## Named vector Each member of a vector can be given a name: ```r age_Simpsons <- c(36, 34, 10, 8, 1) print(age_Simpsons) ``` ``` ## [1] 36 34 10 8 1 ``` ```r *names(age_Simpsons) # currently no names are given to each component ``` ``` ## NULL ``` ```r # Now assign names to each member of the vector names(age_Simpsons) <- c("Homer", "Marge", "Bart", "Lisa", "Maggie") print(age_Simpsons) ``` ``` ## Homer Marge Bart Lisa Maggie ## 36 34 10 8 1 ``` --- ## Vector manipulation Use brackets `[]` to extract components of a vector. ```r *a <- vec1[3] print(a) ``` ``` ## [1] 5 ``` ```r b <- vec2[c(1, 5)] print(b) ``` ``` ## [1] "Joe" "train" ``` -- **Exercise**: extract the last element of `vec1` without viewing its elements. -- ```r vec1[length(vec1)] ``` ``` ## [1] 4 ``` --- ## Vector manipulation, continued Recall the named vector, `age_Simpsons`: ```r print(age_Simpsons) ``` ``` ## Homer Marge Bart Lisa Maggie ## 36 34 10 8 1 ``` What are the ages of the kids? -- ```r age_Simpsons[c(3, 4, 5)] ``` ``` ## Bart Lisa Maggie ## 10 8 1 ``` -- ```r age_Simpsons[c("Bart", "Lisa", "Maggie")] ``` ``` ## Bart Lisa Maggie ## 10 8 1 ``` --- ## Vector manipulation, continued Components of a vector can be extracted using logical values: ```r age_Simpsons[c(TRUE, TRUE, FALSE, TRUE, FALSE)] ``` ``` ## Homer Marge Lisa ## 36 34 8 ``` -- ```r male <- c("Homer", "Bart") age_Simpsons[names(age_Simpsons) %in% male] ``` ``` ## Homer Bart ## 36 10 ``` --- ## Vector manipulation, continued Modifying a vector: ```r m1 <- c("Apple", "Bananana", "Cherry") m1[2] <- "Banana" print(m1) ``` ``` ## [1] "Apple" "Banana" "Cherry" ``` -- Joining vectors: ```r m2 <- c("Pear", "Watermelon") m3 <- c(m1, m2) print(m3) ``` ``` ## [1] "Apple" "Banana" "Cherry" "Pear" "Watermelon" ``` --- ## Special vectors -- part I **Exercise**: compute the sum `\(1 + 2 + 3 + \cdots + 10\)`. -- ```r w1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) sum(w1) ``` ``` ## [1] 55 ``` -- Colon (:) can be used to generate a vector consisting of consecutive numbers. ```r w2 <- 1:10 print(w2) ``` ``` ## [1] 1 2 3 4 5 6 7 8 9 10 ``` ```r sum(w2) ``` ``` ## [1] 55 ``` --- ## Special vectors -- part II **Exercise**: compute the sum `\(2 + 4 + 6 +\cdots + 20\)`. -- ```r # one can use a function seq() # type ?seq to get help *w3 <- seq(2, 20, by = 2) sum(w3) ``` ``` ## [1] 110 ``` -- Alternatively, one can use the previous vector: ```r w4 <- 2*w1 # product of a single number and a vector print(w4) ``` ``` ## [1] 2 4 6 8 10 12 14 16 18 20 ``` ```r sum(w4) ``` ``` ## [1] 110 ``` --- ## Recycling in R Operation between a single value and a vector. ```r num3 <- 3 vec5 <- 1:5 vec6 <- vec5 + num3 print(vec6) ``` ``` ## [1] 4 5 6 7 8 ``` ```r *vec7 <- paste0("Visitor_", vec5) print(vec7) ``` ``` ## [1] "Visitor_1" "Visitor_2" "Visitor_3" "Visitor_4" "Visitor_5" ``` ```r vec8 <- 5 + 2*c(-1, 1) print(vec8) ``` ``` ## [1] 3 7 ``` --- ## Recycling in R, continued **Exercise**: Change "Female" to "F" for consistency. ```r gender <- c("M", "F", "Female", "M", "Female") ``` -- ```r # solution using hardcoding sol1 <- gender sol1[c(3, 5)] <- "F" print(sol1) ``` ``` ## [1] "M" "F" "F" "M" "F" ``` -- ```r # solution without using hardcoding sol2 <- gender sol2[sol2 == "Female"] <- "F" print(sol2) ``` ``` ## [1] "M" "F" "F" "M" "F" ``` --- ## Data frame Vectors of an equal length can be combined to form a data frame. ```r # use of the function data.frame() first_name <- c("Lisa", "John", "Chuck", "Matt") last_name <- c("Simpson", "Smith", "Williams", "June") age_yrs <- c(8, 42, 81, 23) *book <- data.frame(first = first_name, * last = last_name, * age = age_yrs) print(book) ``` ``` ## first last age ## 1 Lisa Simpson 8 ## 2 John Smith 42 ## 3 Chuck Williams 81 ## 4 Matt June 23 ``` ```r *dim(book) # dimensions of a data frame: dim() ``` ``` ## [1] 4 3 ``` --- ## Handling data frames ```r # extract a single element from a data frame book[2, 3] ``` ``` ## [1] 42 ``` ```r # extract a column: use $ *age_extracted <- book$age print(age_extracted) ``` ``` ## [1] 8 42 81 23 ``` ```r # extract a row book[4, ] ``` ``` ## first last age ## 4 Matt June 23 ``` --- ## Retrieving a column from a data frame ```r # use column number method1 <- book[,1] print(method1) ``` ``` ## [1] "Lisa" "John" "Chuck" "Matt" ``` ```r # use "$" to extract a column method2 <- book$first print(method2) ``` ``` ## [1] "Lisa" "John" "Chuck" "Matt" ``` --- ## Adding a variable in a data frame ```r gender <- c("Female", "Male", "Male", "Unknown") book$sex <- gender print(book) ``` ``` ## first last age sex ## 1 Lisa Simpson 8 Female ## 2 John Smith 42 Male ## 3 Chuck Williams 81 Male ## 4 Matt June 23 Unknown ``` Recycling applies to a data frame: ```r book$remark <- "friend" print(book) ``` ``` ## first last age sex remark ## 1 Lisa Simpson 8 Female friend ## 2 John Smith 42 Male friend ## 3 Chuck Williams 81 Male friend ## 4 Matt June 23 Unknown friend ``` --- ## Reading a data file Download a [file](heights.txt) (Chrome users: Menu `\(\rightarrow\)` More Tools `\(\rightarrow\)` Save Page As) and move it to the class directory. ```r # "heights.txt" is a space-separated file with a header # 1375 observations of UK mom/daughter age height pairs *ht <- read.table(file = "heights.txt", header = TRUE, sep = " ") ``` A glance at a data frame: ```r *dim(ht); nrow(ht); ncol(ht) # dimension, number of rows/columns ``` ``` ## [1] 1375 2 ``` ``` ## [1] 1375 ``` ``` ## [1] 2 ``` ```r names(ht) # variable(column) names ``` ``` ## [1] "Mheight" "Dheight" ``` --- ## Inspecting a data frame ```r *head(ht) # first few rows (6 rows by default) ``` ``` ## Mheight Dheight ## 1 59.7 55.1 ## 2 58.2 56.5 ## 3 60.6 56.0 ## 4 60.7 56.8 ## 5 61.8 56.0 ## 6 55.5 57.9 ``` ```r *tail(ht) # last few rows ``` ``` ## Mheight Dheight ## 1370 69.5 70.4 ## 1371 69.1 70.1 ## 1372 65.0 71.6 ## 1373 66.3 71.4 ## 1374 70.8 71.0 ## 1375 63.0 73.1 ``` --- ## List A list is a collection of different types of elements: ```r *li <- list(num = 1.6, seq = 1:5, names = c("Bart", "Lisa", "Homer"), uk.heihgt = ht) names(li) ``` ``` ## [1] "num" "seq" "names" "uk.heihgt" ``` -- .pull-left[ ```r li$num ``` ``` ## [1] 1.6 ``` ```r li$names ``` ``` ## [1] "Bart" "Lisa" "Homer" ``` ] -- .pull-right[ ```r head(li$uk.heihgt) ``` ``` ## Mheight Dheight ## 1 59.7 55.1 ## 2 58.2 56.5 ## 3 60.6 56.0 ## 4 60.7 56.8 ## 5 61.8 56.0 ## 6 55.5 57.9 ``` ]