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:
(1) Create an
R Project in a new folder on your desktop using RStudio.
After doing so, do the following:
Please work in this R Project when answering the remaining questions.
(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
iris dataset and what we know of indexing and logical operators please:
Subset the data frame to only contain the “setosa” species.
Subset the data frame to only contain the observations with
Sepal.Width greater than
(3) Create a vector containing the names of everyone in your breakout room as strings and store it in the object
(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
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
breakoutdata as a stata file, e.g.
breakoutdata as a
breakoutdata as an excel spreadsheet, e.g.
Look at your
Data folder to make sure your files exported into the right place.
(2) Import the
breakout data back into
.dtadata file into object
.xlxsdata file into the object
.csvdata file into the object