Download the homework1.Rmd
file from Canvas or the course website.
Open homework1.Rmd
in RStudio.
Replace the “Your Name Here” text in the author:
field with your own name.
Supply your solutions to the homework by editing homework1.Rmd
.
When you have completed the homework and have checked that your code both runs in the Console and knits correctly when you click Knit HTML
, rename the R Markdown file to homework1_YourNameHere.Rmd
, and submit on Canvas (YourNameHere should be changed to your own name.)
Keystroke | Description |
---|---|
<tab> |
Autocompletes commands and filenames, and lists arguments for functions. |
<up> |
Cycles through previous commands in the console prompt |
<ctrl-up> |
Lists history of previous commands matching an unfinished one |
<ctrl-enter> |
Runs current line from source window to Console. Good for trying things out ideas from a source file. |
<ESC> |
Aborts an unfinished command and get out of the + prompt |
Note: Shown above are the Windows/Linux keys. For Mac OS X, the <ctrl>
key should be substituted with the <command>
(⌘) key.
Instead of sending code line-by-line with <ctrl-enter>
, you can send entire code chunks, and even run all of the code chunks in your .Rmd file. Look under the
Run your code in the Console and Knit HTML frequently to check for errors.
You may find it easier to solve a problem by interacting only with the Console at first.
This homework gets you to create a “Cheat Sheet” that you can refer back to over the course of the semester.
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'dplyr' was built under R version 3.4.1
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
Tip: Note that each of the code blocks in this Problem contain the expression
eval = FALSE
. This tells R Markdown to display the code contained in the block, but not to evaluate it. To check that your answer makes sense, be sure to try it out in the console with various choices of values for the variablex
.
Given a variable x
, write a Boolean expression that will evaluate to TRUE
if the variable x
is equal to 94842
(the numeric value).
# Insert your Boolean expression here
Given a variable x
, write a Boolean expression that will evaluate to TRUE
if the variable x
is not NA
(i.e., is not missing).
# Insert your Boolean expression here
Given a (possibly negative) number x
, write a Boolean expression that returns TRUE
if and only if x
is smaller than -12
or bigger than 29
.
# Insert your Boolean expression here
Given an integer number x
, write a Boolean expression that returns TRUE
if and only if x
is an odd number between -6 and 16 or 102 and 250.
# Insert your Boolean expression here
Tip: Recall the modulus operator we saw in lecture 1: %%
. For integers x
and y
, x %% y
is the remainder of x
divided by y
.
&
, |
) and double (&&
, ||
).One of these operators takes advantage of something called lazy evaluation while the other does not. They also don’t behave the same way when applied to vectors.
Read the help file (help("||")
) and construct some examples to help figure out how the two behave.
To help you get started, try out the following two examples in your console:
# Example: The variable y.prob2a is never defined.
# (Do not define it!)
# What happens when you run this code?
x.prob2a <- 5
(x.prob2a < 10) | (y.prob2a > 2)
(x.prob2a < 10) || (y.prob2a > 2)
# Define vectors
x.prob2a.vec <- c(TRUE, FALSE, FALSE)
y.prob2a.vec <- c(TRUE, TRUE, FALSE)
# Apply various Boolean operations to see what happens
x.prob2a.vec & y.prob2a.vec
x.prob2a.vec && y.prob2a.vec
x.prob2a.vec | y.prob2a.vec
x.prob2a.vec || y.prob2a.vec
Can you explain what’s happening? Write up a brief explanation below.
Replace this text with your explanation.
all()
Two people were asked to give their preferences between two options: [Facebook, Twitter], [Firefox, Chrome], [Mac, PC], [Summer, Winter]. Their results are given below.
alice.prefs <- c("Twitter", "Chrome", "Mac", "Summer")
bob.prefs <- c("Facebook", "Chrome", "PC", "Summer")
Use the all()
function to determine if the two people have identical preferences. (Your code should ouput a single Boolean value, either TRUE
or FALSE
)
# Edit me
any()
Use the any()
function to determine if the two people have any preferences in common. (Your code should output a single Boolean value, either TRUE
or FALSE
)
# Edit me
Let age
be the vector defined below.
age <- c(18, NA, 25, 71, NA, 45, NA, NA, 18)
Write a Boolean expression that checks whether each entry of age
is missing (recall missing values are denoted by NA
). Your expression should return a Boolean vector having the same length as age
.
# Edit me
which()
practiceWrite code that returns the indexes of age
that are missing.
# Edit me
Write code that uses negative indexes and your solution from (a) to return only the values of age
that are not missing. (i.e., your code should result in a vector with elements: 18, 25, 71, 45, 18)
# Edit me
Using the negation operator !
and the is.na()
function, write an expression that returns only the values of age
that are not missing.
# Edit me
which()
practiceFor the next three problem we’ll go back to the cars
data set.
speed <- cars$speed
dist <- cars$dist
Write code to figure out which cars had a stopping distance of 15 feet or more.
# Edit me
which.min
, which.max
practiceUse the which.min()
function to figure out which car had the shortest stopping distance. (Your code should return the car’s index.)
# Edit me
Use the which.max()
function to figure out the speed of the car that had the longest stopping distance. (Your code should return the car’s speed.)
# Edit me
In Lecture 2 we saw how to use the read.table()
function to import the survey data. Now we’ll use a different function. Use the read.csv()
function to import the survey data into a variable called survey
.
# Edit me
Tip: The data file is located at http://www.andrew.cmu.edu/user/achoulde/94842/data/survey_data2019.csv
. Do not download the file. Import the data directly using the URL.
$
notationUse the $
operator to select the TVhours column from the survey
data
# Edit me
Repeat part (b) using [,]
notation. i.e., Use [,]
notation to select the TVhours column from the survey
data by name (i.e., obtain this column by using the name “TVhours” instead of using the column number)
# Edit me
Repeat part (c) with [[]] notation.
# Edit me
Repeat part (d), but this time using single blackets ([ ]
) notation.
(Observe that this returns a new single-column data frame, not just a vector.)
# Edit me
filter()
and select()
practiceUse the filter()
and select()
functions to grab the Program and TV watching information for all PPM students who have no prior programming experience (PriorExp).
# Edit me
Create a bar graph of respondents’ Rexperience.
# Edit me
Replace all occurrences of ???? in the paragraph below with an inline code chunk supplying the appropriate information.
Of the ???? survey respondents, ???? were NOT from the MISM program. We found that ????% of the all students in the class use the Mac OS X operating system. ????% of of PPM students report having Basic competence in R.