Getting Set Up
Before we begin, start a new file with File
\(\rightarrow\) New File
\(\rightarrow\) R Script
. As you work through this sheet in the console in R
, also add (copy/paste) your commands that work into this new file. At the end, save it, and run to execute all of your commands at once.
Creating Objects
Question 1
Work on the following parts:
- a. Create a vector called
me
with two objects, your first name, and your last name. - Call the vector to inspect it.
- Confirm it is a character class vector.
Question 2
Use R’s help functions to determine what the paste()
function does. Then paste together your first name and last name.
Question 3
Create a vector called my_vector
with all the even integers from 2 to 10.
Question 4
Find the mean of my_vector
with mean()
.
Question 5
Take all the integers from 18 to 763,1 then get the mean.
Playing with Data
For the following questions, we will use the diamonds
dataset, included as part of ggplot2
.
Question 6
Install ggplot2
.
Question 7
Load ggplot2
with the library()
command.
Question 8
Get the str
ucture of the diamonds data frame. What are the different variables and what kind of data does each contain?
Question 9
Get summary statistics separately for carat
, depth
, table
, and price
.
Question 10
color
, cut
, and clarity
are categorical variables (factors). Use the table()
command to generate frequency tables for each.
Question 11
Now rerun the summary()
command on the entire data frame.
Question 12
Now look only at (subset) the first 4 diamonds in the dataset.
Question 13
Now look only at (subset) the third and seventh diamond in the dataset.
Question 14
Now look only at (subset) the second column of the dataset.
Question 15
Do this again, but look using the $
to pull up the second column by name.
Question 16
Now look only at diamonds that have a carat
greater than or equal to 1.
Question 17
Now look only at diamonds that have a VVS1 clarity
.
Question 18
Now look only at dimaonds that have a color of E, F, I, and J.
Question 19
Now look only at diamonds that have a carat
greater than or equal to 1 and a VVS1 clarity
.
Question 20
Get the average price of diamonds in question 18.2
Question 21
What is the highest price for a diamond with a 1.0 carat, D color, and VVS1 clarity?
Execute your R Script
Save the R Script you created at the beginning and (hopefully) have been pasting all of your valid commands to. This creates a .R file wherever you choose to save it to. Now looking at the file in the upper left pane of R Studio look for the button in the upper right corner that says Run. Sit back and watch R
redo everything you’ve carefully worked on, all at once.