library(tidyverse)
## ── Attaching packages ──────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ─────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()

1. Changing the author field and file name.

(a) Change the author: field on the Rmd document from Your Name Here to your own name.
(b) Rename this file to “lab02_YourHameHere.Rmd”, where YourNameHere is changed to your own name.

2. Data practice

In class we imported the survey data using the read.table() function. (Note: on Homework 1 you’ll be asked to use the read.csv() function instead.)

This is the code we used:

survey <- read.table("http://www.andrew.cmu.edu/user/achoulde/94842/data/survey_data2020.csv", header=TRUE, sep=",") 
(a) How many survey respondents are from MISM or Other?
sum(survey[["Program"]] == "MISM" | survey[["Program"]] == "Other")
## [1] 19
(b) What % of survey respondents are from PPM?
100 * sum(survey[["Program"]] == "PPM") / nrow(survey)
## [1] 66.66667

3. Index practice

(a) Use $ notation to pull the OperatingSystem column from the survey data
survey$OperatingSystem
##  [1] Windows    Mac OS X   Windows    Windows    Windows    Mac OS X  
##  [7] Mac OS X   Mac OS X   Windows    Windows    Windows    Linux/Unix
## [13] Mac OS X   Windows    Windows    Windows    Windows    Windows   
## [19] Mac OS X   Windows    Windows    Mac OS X   Mac OS X   Windows   
## [25] Mac OS X   Windows    Windows    Mac OS X   Mac OS X   Windows   
## [31] Windows    Windows    Windows    Mac OS X   Mac OS X   Windows   
## [37] Mac OS X   Windows    Windows    Windows    Mac OS X   Windows   
## [43] Windows    Windows    Mac OS X   Windows    Mac OS X   Windows   
## [49] Linux/Unix Mac OS X   Windows    Windows    Windows    Windows   
## [55] Windows    Windows    Mac OS X  
## Levels: Linux/Unix Mac OS X Windows
(b) Do the same thing with [,] notation, referring to OperatingSystem by name
survey[, "OperatingSystem"]
##  [1] Windows    Mac OS X   Windows    Windows    Windows    Mac OS X  
##  [7] Mac OS X   Mac OS X   Windows    Windows    Windows    Linux/Unix
## [13] Mac OS X   Windows    Windows    Windows    Windows    Windows   
## [19] Mac OS X   Windows    Windows    Mac OS X   Mac OS X   Windows   
## [25] Mac OS X   Windows    Windows    Mac OS X   Mac OS X   Windows   
## [31] Windows    Windows    Windows    Mac OS X   Mac OS X   Windows   
## [37] Mac OS X   Windows    Windows    Windows    Mac OS X   Windows   
## [43] Windows    Windows    Mac OS X   Windows    Mac OS X   Windows   
## [49] Linux/Unix Mac OS X   Windows    Windows    Windows    Windows   
## [55] Windows    Windows    Mac OS X  
## Levels: Linux/Unix Mac OS X Windows
(c) Repeat part (b), this time referring to OperatingSystem by column number
survey[, 4]
##  [1] Windows    Mac OS X   Windows    Windows    Windows    Mac OS X  
##  [7] Mac OS X   Mac OS X   Windows    Windows    Windows    Linux/Unix
## [13] Mac OS X   Windows    Windows    Windows    Windows    Windows   
## [19] Mac OS X   Windows    Windows    Mac OS X   Mac OS X   Windows   
## [25] Mac OS X   Windows    Windows    Mac OS X   Mac OS X   Windows   
## [31] Windows    Windows    Windows    Mac OS X   Mac OS X   Windows   
## [37] Mac OS X   Windows    Windows    Windows    Mac OS X   Windows   
## [43] Windows    Windows    Mac OS X   Windows    Mac OS X   Windows   
## [49] Linux/Unix Mac OS X   Windows    Windows    Windows    Windows   
## [55] Windows    Windows    Mac OS X  
## Levels: Linux/Unix Mac OS X Windows
(d) Repeat part (b), this time using the select() function.
select(survey, OperatingSystem)
##    OperatingSystem
## 1          Windows
## 2         Mac OS X
## 3          Windows
## 4          Windows
## 5          Windows
## 6         Mac OS X
## 7         Mac OS X
## 8         Mac OS X
## 9          Windows
## 10         Windows
## 11         Windows
## 12      Linux/Unix
## 13        Mac OS X
## 14         Windows
## 15         Windows
## 16         Windows
## 17         Windows
## 18         Windows
## 19        Mac OS X
## 20         Windows
## 21         Windows
## 22        Mac OS X
## 23        Mac OS X
## 24         Windows
## 25        Mac OS X
## 26         Windows
## 27         Windows
## 28        Mac OS X
## 29        Mac OS X
## 30         Windows
## 31         Windows
## 32         Windows
## 33         Windows
## 34        Mac OS X
## 35        Mac OS X
## 36         Windows
## 37        Mac OS X
## 38         Windows
## 39         Windows
## 40         Windows
## 41        Mac OS X
## 42         Windows
## 43         Windows
## 44         Windows
## 45        Mac OS X
## 46         Windows
## 47        Mac OS X
## 48         Windows
## 49      Linux/Unix
## 50        Mac OS X
## 51         Windows
## 52         Windows
## 53         Windows
## 54         Windows
## 55         Windows
## 56         Windows
## 57        Mac OS X
(e) Use filter() and select() together to get the Program and Rexperience of every student who watched at least 10 hours of TV last week.
survey %>%
  filter(TVhours >= 10) %>%
  select(Program, Rexperience)
##    Program          Rexperience
## 1      PPM           Never used
## 2      PPM           Never used
## 3      PPM Installed on machine
## 4      PPM           Never used
## 5      PPM     Basic competence
## 6      PPM     Basic competence
## 7      PPM           Never used
## 8     MISM          Experienced
## 9    Other     Basic competence
## 10     PPM     Basic competence
## 11     PPM           Never used
## 12   Other           Never used
## 13     PPM     Basic competence
## 14   Other     Basic competence
## 15     PPM     Basic competence
## 16     PPM Installed on machine
## 17     PPM     Basic competence
## 18     PPM Installed on machine
## 19   Other           Never used

4. Optional practice

Try re-running as much of the lecture code as you have time for, taking time to understand what is happening in each line of code. You can enter the code directly into the Console. (Optional: you may enter your code in the code chunk below.)

Tip: Instead of copying and pasting code, practice typing it yourself. This will help you to learn the syntax.

# Edit me (optional)