The purpose of this first assignment is to demonstrate that you have downloaded the “base R” and “RStudio” statistical programs and can open a SPSS datafile in RStudio.
This document was created as an R Markdown file. You will learn about R Markdown files later in the assignment. First, I want to familiarize you with the RStudio interface.
install.packages()
and
library()
commands to install and load packages in Rhere()
function
for simple, reproducible file directory referencingread_spss()
function from “haven” package, then assign it to an object in the R
environment using an assignment (<-
) operatorFor all assignments in this class, you must have access to a computer and Microsoft Word. As a TAMUSA student you have access to the Microsoft 365 suite, which includes 1TB of storage on OneDrive. You may find instructions for how to access and install programs (such as Microsoft Word) from Microsoft 365 in the Resources & Supplemental Texts folder under the Course Content tab in Blackboard.
For this and all future assignments, I will assume you are working on your own computer and are downloading base R and RStudio (free open source) programs on your own computer. If you experience technical difficulties, remember that you can call ITS at 210-784-4357 or email them at helpdesk@tamusa.edu for help.
Also, for this and all future assignments, you MUST type all commands in by hand. Do not copy & paste except for troubleshooting purposes (i.e., if you cannot figure out what you mistyped).
Early on, you may have a lot of trouble getting your code to run due to minor typos. This is normal. Remember, you are learning to read and write a new (coding) language. As with learning any new languages, we learn from practice - and from correcting our mistakes.
Goal: Create new CRIM5305_L_LastName folder on your local computer, then download datasets and save to your new folder (i.e., “L” for “local” computer folder).
(Note: When following instructions, always substitute “LastName” for your own last name! Also, substitute YEAR-MO-DY for the actual date. E.g., 2023-01-27_Ducate_CRIM5305_Assign01)
CRIM5305_L_LastName
in a location that is easy to access
(e.g., on Desktop or in “My Documents”).Datasets
(CRIM5305_L_LastName > Datasets) NOTE: To download, you may need to right click and select “Save link as…”
Do NOT try to open the datasets after you have downloaded them.
If your computer asks which program to open them with, press CANCEL. Do
not open them in Microsoft Word. Simply download them. We will open them
in RStudio in Part 4.
CRIM5305_L_LastName
folder. Name the file:
YEAR-MO-DY_LastName_CRIM5305_Assign01_folder-screenshotCRIM5305_C_LastName
in your cloud storage (e.g., iCloud, Dropbox, OneDrive).
If you have your cloud storage synced to your computer, you can
work out of this folder instead of your CRIM5305_L_LastName
folder.
If you choose to work out of this folder instead of your local
folder (which I recommend), be sure to copy your Datasets
folder into this folder as well.
Goal: Download R & R Studio; Open R Markdown; Knit first RMD file
In this section, you will begin by downloading and installing two programs on your computer: base R and RStudio.The first program, R, is simultaneously a computer coding language and a statistical software program. The second, RStudio, is an integrated development environment (IDE) that provides a more user-friendly interface for working with the R program. Throughout this course, you will learn to write and submit R code in RStudio to run statistical commands in the R program. After installing R & RStudio, you will run some simple commands to familiarize yourself with the basic features of the program and install two R packages.
Opening a new R Markdown file
OK
to create your new R Markdown file. It should
look like this:Your new R Markdown file
Keep the YAML header; delete the template
<Enter>
on your keyboard to leave a blank
line between the header and the first line of text.<Enter>
and, on the next line (line 9), type:
### Learning R Markdown
<Enter>
to leave another blank line (line
10)Your first R Markdown file
knit
button
comment
out any code chunks that are unfinished or
incorrect, otherwise the document will not “Knit”. You will learn about
commenting
later in the assignment.Your first knitted html document using R Markdown
Goal: Insert R code chunk in R Markdown; Install
haven
(withtidyverse
) andhere
packages then load in R
Create a second-level header in R Markdown (hereafter, “RMD”)
file titled: “Load Libraries”
Insert an R code chunk
install.packages("tidyverse")
into the code chunk
and hit the right-pointing green arrow on the right side of the chunk.
Insert R code chunk
Insert R code chunk
haven
package by typing,
selecting, and running the command library("haven")
in your
RMD file.
For more information about the haven package, see: https://cran.r-project.org/web/packages/haven/readme/README.html
Note: The haven
package is part
of a much larger suite of packages known as the tidyverse
,
which you have just installed.
here
package.
Type install.packages("here")
into your RMD file,
hit RUN or select (highlight) this text line, and RUN the selection
(Windows: CTRL + Enter; Mac: cmd + Enter).
here
package by typing,
selecting, and running library("here")
into your RMD file.
The here
package will help you start a replicable
project-oriented workflow from the beginning. Here is how it will work
once installed:
CRIM5305_L_LastName
folder.CRIM5305_L_LastName
folder to
automatically open it with RStudio. When you do this, your “working
directory,” i.e., the place R looks for files by default, will
automatically be set to your CRIM5305_L folder.here
package will then make it easy to find and
call objects (e.g., SPSS dataset) in subfolders of your working
directory (e.g., in “Datasets” folder).haven
and here
packages loaded properly.
First, find the “Packages” tab (see “blue pane” in AS blog).
Next, scroll down through the listed packages until you find the haven and here package entries.
If there are checkmarks next to both entries, it worked!
A checkmark means the R package is successfully loaded
Goal: Save RMD; Use
haven
package to open SPSS file in RStudio
Save R Script and Markdown files in top-level folder
here
package to work
properly.here
package by default will set the
CRIM5305_L folder as the top-level working directory#comments
to R code and load haven
and
here
packages
In your RMD, place hashmarks (#) in front of both
install.packages
commands. The haven
and
here
packages should already be installed, so you should
not need to run these lines again. Placing a hashmark in front of a
command – or in front of any text in R code – will create a “comment”
that will not be run as a command in R.
After commenting out the #install.packages
commands,
run both library
commands to load the haven
and here
packages. Though already installed, you will
need to reload all packages that you want to use at the beginning of
each R session.
library
commands),
try running the entire document instead of only a single code
chunk.Check the “Packages” tab to make sure there are checkmarks by the
haven
and here
packages.
Your RMD and RStudio session should now look similar to this:
RStudio session with comments and loaded packages
here
package. It confirms the here
package has
set my CRIM5305_L_LastName folder (i.e., for me, CRIM5305_L_Ducate) as
the top-level working directory.read_spss(here("Datasets","Monitoring the Future 2013 grade 10_0.sav"))
read_spss()
is a haven
package command
that allows R to read SPSS datafilesread_spss()
parentheses, we need to specify
the file location and the filename we wish to open, separated by a
comma.here(“Datasets”,
which uses the here package to locate our
“Datasets” folder. By using the here package, your R code should work
for anyone on any computer as long as they have the exact same file
structure (e.g., you can work from local or cloud storage, or work from
different computers)Monitoring the Future 2013 grade 10_0.sav
file in the
“Datasets” folder to open.
read_spss()
command. You should see a small
snapshot of the data in your R viewer (right-hand side of RStudio,)
which looks something like this:readspss() command shows snapshot of data in R Console
You just read a dataset into RStudio!
However, you do not want the dataset in the R Console – you want to be able to call the dataset as an object on which you can do things, such as summarize or correlate variables (later). To do this, you need to assign the dataset to an object in the R Environment. Note that your R Environment tab is still empty and looks like this:
Empty Environment after reading in SPSS data and before creating a data object
MF2013g10data <-
before the
read_spss()
command.
MF2013g10data
is the name we are giving to the
data object we are creating.<-
command tells R to place whatever follows the
text arrow (in this case, the dataset read by the
read_spss()
command) into our MF2013g10data
object.
mydata
). However, it is good coding practice
to be systematic in creating concise yet informative names. The name I
selected reminds me that the object is the
Monitoring the Future
2013 grade 10
data.Example RMD for reading SPSS Data into R Object
Goal: Submitting your first assignment
Final Knitted Document for Assignment 1
Main Folder after Assignment 1 completion
After completing assignment #1, …
install.packages()
and
library()
commands to install and load packages in R?#
) to comment out a
section of R code so it does not run?here
package and
here()
function for simple, reproducible file directory
referencing?read_spss()
function from “haven” package, then assign it
to an object in the R environment using an assignment
(<-
) operator?