1.2 — Meet R — R Practice

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


Work on the following parts:


Use R’s help functions to determine what the paste() function does. Then paste together your first name and last name.


Create a vector called my_vector with all the even integers from 2 to 10.


Find the mean of my_vector with mean().


Take all the integers from 18 to 763,Hint: use the : operator to create a sequence from a starting number to an ending number

then get the mean.

Playing with Data

For the following questions, we will use the diamonds dataset, included as part of ggplot2.


Install ggplot2.


Load ggplot2 with the library() command.


Get the structure of the diamonds data frame. What are the different variables and what kind of data does each contain?


Get summary statistics separately for carat, depth, table, and price.


color, cut, and clarity are categorical variables (factors). Use the table() command to generate frequency tables for each.


Now rerun the summary() command on the entire data frame.


Now look only at (subset) the first 4 diamonds in the dataset.


Now look only at (subset) the third and seventh diamond in the dataset.


Now look only at (subset) the second column of the dataset.


Do this again, but look using the $ to pull up the second column by name.


Now look only at diamonds that have a carat greater than or equal to 1.


Now look only at diamonds that have a VVS1 clarity.


Now look only at dimaonds that have a color of E, F, I, and J.


Now look only at diamonds that have a carat greater than or equal to 1 and a VVS1 clarity.


Get the average price of diamonds in question 18.Hints: use your subset command as an argument to the mean function. You will not need a comma here because you are looking for a single row.


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.