Instructions

The following assignment seeks to reinforce some of the concepts we honed in class. Only through repeated practice will you being to really hone your skills as an R user. In a new R script, doing the following:


R Projects


(1) Create an R Project in a new folder on your desktop using RStudio.

After doing so, do the following:

  • Check the working directory of the new R session started in your project.
  • Close RStudio and move the folder containing your R Project off of your Desktop and into a new folder.
  • Fire RStudio back up by clicking on the project icon.
  • Check the working directory. Has it changed from what it was previously?

Please work in this R Project when answering the remaining questions.


Objects and Data Structures


(1) Subset a vector

The following line of code simulates one thousand random draws from a normal distribution (with a mean of 0 and a variance of 1). Please subset this vector so that only values above the mean are retained.

vec <- rnorm(n = 1000,mean = 0, sd = 1)

Next, subset the vector so that only values between -.5 and .5 are retained.


(2) Subset a data frame

For the following example, we are going to use the iris example dataset, which is inherent in R.

?iris # to see what it's all about

Using the iris dataset and what we know of indexing and logical operators please:

  1. Subset the data frame to only contain the “setosa” species.

  2. Subset the data frame to only contain the observations with Sepal.Width greater than 3.1.


(3) Create a vector containing the names of everyone in your breakout room as strings and store it in the object student_names.


(4) Create a data frame containing one variable student_name that contains the name of everyone in your breakout room. Use the vector you created in the last question to create generate the variable. Save that data frame to an object named breakout.


Exporting and Importing data


Note that we’ll use the following packages to import/export different data formats, so make sure they’re (a) installed on your computer and (b) imported into your current R Session.

install.packages("readr")
install.packages("haven")
install.packages("readxl")


(1) Export your breakout data into multiple file formats.

Let’s EXPORT your breakout data into a number of different formats and save it to a new folder called “Data”.1

  1. Create a folder called “Data” in your R Project folder.
  2. export the breakout data as a stata file, e.g. breakout_group.dta.
  3. export the breakout data as a .csv file, e.g. breakout_group.csv.
  4. export the breakout data as an excel spreadsheet, e.g. breakout_group.xlxs.

Look at your Data folder to make sure your files exported into the right place.


(2) Import the breakout data back into R.

  1. assign the .dta data file into object S1
  2. assign the .xlxs data file into the object S2
  3. assign the .csv data file into the object S3

  1. Confused? See the lecture slides for guidance on how to do this. ↩︎