getwd()
R Projects & Importing Data
0.1 Learning Objectives
- Describe what an R Project is
- Outline how an R Project affects the file paths used to read in data
- Read in data from common formats into R
▶️ Watch Videos: 14 minutes
📖 Readings: 15-20 minutes
💻 Activities: 30 minutes
✅ Check-ins: 1
1 Directories, Paths, and Projects
Last week, we learned how to organize our personal files and locate them using absolute and relative file paths. The idea of a “base folder” or starting place was introduced as a working directory. In R
, there are two ways to set up your file path and file system organization:
- Set your working directory in
R
(do not recommend) - Use R Projects (preferred!)
1.1 Working Directories in R
To find where your working directory is in R
, you can either look at the top of your console or type getwd()
into your console.
Although it is not recommended, you can set your working directory in R
with setwd()
:
setwd("/path/to/my/assignment/folder")
R
A quick warning on file paths is that Mac / Linux and Windows differ in the direction of their backslash to separate folder locations. Mac/Linux use /
(e.g. STAT331/Week1
) while Window’s uses \
(e.g. STAT331\Week1
).
1.2 R Projects
📖 Required Reading: Workflow (R Projects)
If you open up your folder from Lab 1, you will notice that you have a blue cube (an R Project) living in this folder! The R Project lives in this folder for two reasons, (1) when you copied the Lab 1 GitHub repository you told RStudio you wanted to make a new R Project, and (2) in order for RStudio to talk with GitHub you need to use an R Project.
However, if you look at your STAT331 folder (the one that lives in your Documents folder), you should not see a blue cube since we never created an R Project in this folder. Let’s do that now!
To add a R Project to an existing folder on your computer (e.g., you already created a folder for this class), first open RStudio on your computer and click File > New Project
, then:
Then, browse on your computer to where you saved your STAT331 folder last week. This should be in your Documents folder, similar to where mine is saved!
Once you click “Create Project”, your existing folder, STAT331
should now contain a STAT331.Rproj
file. This is your new “home” base for this class - whenever you refer to a file with a relative path it will begin to look for it here.
If you would like a list of step-by-step instructions for this process, feel free to look at this file: Creating a Project in an Existing Directory
1.3 ✅ Check-in 2.2: RStudio Projects
Take a screenshot of your class directory, showing the R Project living inside your STAT331 folder.
2 Loading Data Into R
2.1 How do I load my data?
2.2 Where does my data live?
2.3 ✅ Check-in 2.3: Loading Data
For this check-in you are asked to work through reading in different data sets. You are expected to create your own Quarto document to complete this activity.
The folder Age_Data contains several data sets with the names and ages of five individuals. The data sets are stored as different file types. Download Ages_Data.zip here, make sure to unzip the folder, save these in a reasonable place (e.g., STAT331 > Week 2 > Checkins
or STAT331 > Checkins > Week 2
).
You will need to extract the contents of the ages.zip
file, that means you will need to uncompress the files from the folder for RStudio to know where to get the data from.
Once you have the data saved (and extracted) in your STAT 331 folder, preferably in the Week 2 subfolder, use the readr
and readxl
packages to complete the following exercises.
- Load the appropriate packages for reading in data.
- Read in the data set ages.csv
- Read in the data set ages_tab.txt
- Read in the data set ages_mystery.txt
- Read in the data set ages.xlsx
- Find a way to use
read_csv()
to read ages.csv with the variable “Name” as a factor data type and “Age” as a character data type.
Once you’ve finished coding these exercises, head over to Canvas and fill in the “gap” code I’ve provided. For reference, I would recommend you store the Ages_Data
similar to how I have: