Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rbootcamp Day 1

402 views

Published on

This is the first day of the 5-day long workshop covering essentials of R programming language

Published in: Data & Analytics

Rbootcamp Day 1

  1. 1. R Studio R Basics Operators Packages Importing DataCamp RBootcamp Day 1 Olga Scrivner and Jefferson Davis Assistant Jivitesh Poojary 1 / 50
  2. 2. R Studio R Basics Operators Packages Importing DataCamp Sponsors This RBootcamp is sponsored by Center of Excellence for Women in Technology (CEWiT) and Social Science Research Commons (SSRC) 2 / 50
  3. 3. R Studio R Basics Operators Packages Importing DataCamp Outline 1 Intro to RStudio 2 Using R scripts 3 Installing packages 4 R objects Data types Vectors Lists 5 Getting help 3 / 50
  4. 4. R Studio R Basics Operators Packages Importing DataCamp R software R is a free software for statistical analysis, text mining and graphics. To install R on Window: 1 Download the binary file for R https://cran. r-project.org/bin/windows/base/R-3.3.1-win.exe 2 Open the downloaded .exe file and Install R To install R on Mac: 1 Download the appropriate version of .pkg file https://cran.r-project.org/bin/macosx/ 2 Open the downloaded .pkg file and Install R 4 / 50
  5. 5. R Studio R Basics Operators Packages Importing DataCamp R Studio RStudio is a free user interface for R. 1 Install the appropriate RStudio version https: //www.rstudio.com/products/rstudio/download/ 2 Run it to install R-studio 5 / 50
  6. 6. R Studio R Basics Operators Packages Importing DataCamp R Studio Structure For more details - see handout RStudio101 (by Oscar Torres-Reyna) http://dss.princeton.edu/training/RStudio101.pdf6 / 50
  7. 7. R Studio R Basics Operators Packages Importing DataCamp Organizing Your Files Option 1 Create new script / Open existing script Set up your working directory Keep your datafiles in this directory (easy access) Or use command file.choose() Or remember the path to datafiles 7 / 50
  8. 8. R Studio R Basics Operators Packages Importing DataCamp Organizing Your Files Option 1 Create new script / Open existing script Set up your working directory Keep your datafiles in this directory (easy access) Or use command file.choose() Or remember the path to datafiles Option 2 Create new project/ Open existing project Do not have to set up working directory Keep your datafiles in the project directory Do not have to remember the path to datafiles 7 / 50
  9. 9. R Studio R Basics Operators Packages Importing DataCamp Creating Projects 8 / 50
  10. 10. R Studio R Basics Operators Packages Importing DataCamp Creating Projects 8 / 50
  11. 11. R Studio R Basics Operators Packages Importing DataCamp Creating R Script 9 / 50
  12. 12. R Studio R Basics Operators Packages Importing DataCamp Saving R Script 10 / 50
  13. 13. R Studio R Basics Operators Packages Importing DataCamp Closing and Opening Scripts Close R File: File → Close Open R File: File → Open 11 / 50
  14. 14. R Studio R Basics Operators Packages Importing DataCamp Editing Script: Font and Size 12 / 50
  15. 15. R Studio R Basics Operators Packages Importing DataCamp RStudio - Full View 13 / 50
  16. 16. R Studio R Basics Operators Packages Importing DataCamp Learning R Syntax variable stores values Assignment operator: <- x <- 5 y <- 6 A valid name for variable must start with a letter. Name can contain letters, numbers, underscores, and dot. Valid names Invalid names mydata my data mydata2 my.data mydata! my data 2mydata .mydata 14 / 50
  17. 17. R Studio R Basics Operators Packages Importing DataCamp Script Flow 1 Create two variables x <- 5 y <- 6 2 run executes commands: - Place cursor anywhere on the first line - click run - Place cursor on the second line - click run 3 Console displays the execution 4 Right top - Environment stores objects - History stores commands 15 / 50
  18. 18. R Studio R Basics Operators Packages Importing DataCamp Values 1 Change value of y to 6.5 2 Examine objects in environment 16 / 50
  19. 19. R Studio R Basics Operators Packages Importing DataCamp Comments 1 Comments are not executed 2 Comments are preceded by # (hash tag) 3 Type a comment above your first line of code 17 / 50
  20. 20. R Studio R Basics Operators Packages Importing DataCamp Print() Function print() prints the value into your console Inside the parenthesis you type the name of your variable Examine the output in the console 18 / 50
  21. 21. R Studio R Basics Operators Packages Importing DataCamp Characters versus Numeric Values Numbers are without quotation marks: x <- 5 Characters are enclosed in quotation marks: z <-“a” 19 / 50
  22. 22. R Studio R Basics Operators Packages Importing DataCamp Characters versus Numeric Values Numbers are without quotation marks: x <- 5 Characters are enclosed in quotation marks: z <-“a” Arithmetic operations with numerics In the console type x*y, press enter In the console type z*w, press enter 19 / 50
  23. 23. R Studio R Basics Operators Packages Importing DataCamp Logical Values 1 TRUE, FALSE - upper case, no quotes 2 Add comment # logical values 20 / 50
  24. 24. R Studio R Basics Operators Packages Importing DataCamp Data Types 1 Data types: Logical Numeric Character 2 Function class() identifies the class type 3 Type in the script 4 Examine the console 21 / 50
  25. 25. R Studio R Basics Operators Packages Importing DataCamp Vector - Basic Types Vector: A sequence of data elements of the same basic type Numeric c(2, 3, 5) Logical c(TRUE, FALSE, TRUE) Character string c("aa", "bb", "cc") 22 / 50
  26. 26. R Studio R Basics Operators Packages Importing DataCamp Vector In the script create two vectors: Examine the environment 23 / 50
  27. 27. R Studio R Basics Operators Packages Importing DataCamp Length Function length() of a vector length(v1) Create a vector with words: mywords <-c(“These”, “are”,“my”,“words”) 1 How many words in mywords? 24 / 50
  28. 28. R Studio R Basics Operators Packages Importing DataCamp Index Slicing 1. [1:3] - consecutive elements: one, two, three 2. [c(1,3)] - only the elements one and three 3. [-2] - all except the element number two Extract the first and the second elements Extract all except the first element Extract the first and the fourth elements 25 / 50
  29. 29. R Studio R Basics Operators Packages Importing DataCamp Indexing How to extract certain elements from a vector? What is the first word in mywords? - mywords[1] What are the first and second words in mywords? - mywords[1:2] What are the first and third words in mywords? - mywords[c(1,3)] 26 / 50
  30. 30. R Studio R Basics Operators Packages Importing DataCamp Combining Vectors - Strings vector1 <- c("my", "first", "vector") vector2 <- c("my", "second", "vector") vector3 <- c(vector1, vector2) print(vector3) 27 / 50
  31. 31. R Studio R Basics Operators Packages Importing DataCamp Vectors - Arithmetic Operations Click RUN to execute each line v1 <- c(1, 3, 6) v2 <- c(2, 4, 6) v1*v2 v1+v2 v1/v2 vector1*vector2 - what will happen? 28 / 50
  32. 32. R Studio R Basics Operators Packages Importing DataCamp Vectors - Arithmetic Operations Click RUN to execute each line v1 <- c(1, 3, 6) v2 <- c(2, 4, 6) v1*v2 v1+v2 v1/v2 vector1*vector2 - what will happen? 28 / 50
  33. 33. R Studio R Basics Operators Packages Importing DataCamp Vectors - Arithmetic Operations Click RUN to execute each line v1 <- c(1, 3, 6) v2 <- c(2, 4, 6) v1*v2 v1+v2 v1/v2 vector1*vector2 - what will happen? vector3 <- c(vector1, vector2) 28 / 50
  34. 34. R Studio R Basics Operators Packages Importing DataCamp Vectors - paste paste(vector1, "+", vector2, sep = " ") paste(vector1, "+", vector2, sep = "") paste(vector1, "+", vector2, collapse = " ") 29 / 50
  35. 35. R Studio R Basics Operators Packages Importing DataCamp Usefulness of paste - Create a Plot Title Scenario: You are going to create a plot with x (Age Groups) and y (Frequency) with the following title My plot: Frequency of Age Groups y <- "Frequency" x <- "Age Groups" title <- "My plot:" c(title,y,"of",x) 30 / 50
  36. 36. R Studio R Basics Operators Packages Importing DataCamp Usefulness of paste - Create a Plot Title Scenario: You are going to create a plot with x (Age Groups) and y (Frequency) with the following title My plot: Frequency of Age Groups y <- "Frequency" x <- "Age Groups" title <- "My plot:" c(title,y,"of",x) paste(title,y,"of",x,collapse=" ") 30 / 50
  37. 37. R Studio R Basics Operators Packages Importing DataCamp Lists List: a vector that can contain different types mylist <- list(vector1, v1) print(mylist) [[ ]] - index for lists [ ] - index for vectors 31 / 50
  38. 38. R Studio R Basics Operators Packages Importing DataCamp List versus Vector Vectors contain the objects of the same type: - v1 <- c(“a”,“b”,“c”) - v2 <- c(1,2,3,4) Lists contain different types of objects Vector uses c() function List uses list() function Create mylist: 32 / 50
  39. 39. R Studio R Basics Operators Packages Importing DataCamp List versus Vector Vectors contain the objects of the same type: - v1 <- c(“a”,“b”,“c”) - v2 <- c(1,2,3,4) Lists contain different types of objects Vector uses c() function List uses list() function Create mylist: miniquiz: What are the data types in mylist? 32 / 50
  40. 40. R Studio R Basics Operators Packages Importing DataCamp Indexing List 1 Print list: print(mylist) 33 / 50
  41. 41. R Studio R Basics Operators Packages Importing DataCamp Indexing List 1 Print list: print(mylist) 2 Remember vector indices [ ]? 3 List will use [[ ]] 4 Type mylist[[1]] 5 Type mylist[[7]] 33 / 50
  42. 42. R Studio R Basics Operators Packages Importing DataCamp Indexing List 1 Print list: print(mylist) 2 Remember vector indices [ ]? 3 List will use [[ ]] 4 Type mylist[[1]] 5 Type mylist[[7]] 6 How to access the first number inside the list object? 33 / 50
  43. 43. R Studio R Basics Operators Packages Importing DataCamp Indexing List 1 Print list: print(mylist) 2 Remember vector indices [ ]? 3 List will use [[ ]] 4 Type mylist[[1]] 5 Type mylist[[7]] 6 How to access the first number inside the list object? 7 mylist[[7]][1] 33 / 50
  44. 44. R Studio R Basics Operators Packages Importing DataCamp Operators: Arithmetic 34 / 50
  45. 45. R Studio R Basics Operators Packages Importing DataCamp Operators: Logical 35 / 50
  46. 46. R Studio R Basics Operators Packages Importing DataCamp Operators: Logical 36 / 50 a <- 1 b <- 2 a > b a <= 2 a != b a == b
  47. 47. R Studio R Basics Operators Packages Importing DataCamp Operators: Logical 36 / 50 a <- 1 b <- 2 a > b a <= 2 a != b a == b
  48. 48. R Studio R Basics Operators Packages Importing DataCamp Installing Packages In your bottom left window - go to Packages 37 / 50
  49. 49. R Studio R Basics Operators Packages Importing DataCamp Selecting Packages 38 / 50
  50. 50. R Studio R Basics Operators Packages Importing DataCamp Package = Library In your Packages window scroll down until you see languageR and click inside the box: 39 / 50
  51. 51. R Studio R Basics Operators Packages Importing DataCamp Package Content To access package description and its content, click on the package name. New window Help will open up: 40 / 50
  52. 52. R Studio R Basics Operators Packages Importing DataCamp Accessing Info from Packages Scroll down and select languageR-package You will see the list of available functions from this package 41 / 50
  53. 53. R Studio R Basics Operators Packages Importing DataCamp Quick Help Type in the console (bottom left): ?length Instead of Run - click enter-key 42 / 50
  54. 54. R Studio R Basics Operators Packages Importing DataCamp File Formats 1 CSV, Excel Movie metadata.csv 2 TXT NY Times.txt 3 PDF Article.pdf 43 / 50
  55. 55. R Studio R Basics Operators Packages Importing DataCamp CSV, Excel, SAS, SPSS Data 44 / 50
  56. 56. R Studio R Basics Operators Packages Importing DataCamp CSV 45 / 50
  57. 57. R Studio R Basics Operators Packages Importing DataCamp CSV Data Close data view: colnames(movie metadata) nrow(movie metadata) 46 / 50
  58. 58. R Studio R Basics Operators Packages Importing DataCamp Import Text Files - NY Times file path <- file.choose() myfile <- scan(file path, what = "character", sep = "n",blank.lines.skip = TRUE) head(myfile) 47 / 50
  59. 59. R Studio R Basics Operators Packages Importing DataCamp Import PDF Files - Article install.packages("pdftools") library(pdftools) file path2 <- file.choose() myfile2 <- unlist(lapply(file path2, pdf text)) text <- paste(myfile2, collapse = " ") head(text) 48 / 50
  60. 60. R Studio R Basics Operators Packages Importing DataCamp Practice-DataCamp 1 Sign up for a free DataCamp.com account 2 Link to the group https://www.datacamp.com/groups/ 40456ec7289f29ce125860d0dc42d2d12ee15630/ invite 3 Go to groups 4 Select assignments 5 Go to the course RBootcamp day 1 49 / 50
  61. 61. R Studio R Basics Operators Packages Importing DataCamp Practice-DataCamp 1 RBootcamp day 1 2 Complete all practice to receive points! 3 If you get a pop-up message about joining a payed membership, refresh the page and continue the exercise. 50 / 50

×