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
Using the 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.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
.
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
breakout
data as a stata file, e.g. breakout_group.dta
.breakout
data as a .csv
file, e.g. breakout_group.csv
.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
.
.dta
data file into object S1
.xlxs
data file into the object S2
.csv
data file into the object S3
Confused? See the lecture slides for guidance on how to do this. ↩︎