Loading Data in RStudio
The Goal
We have been using data that can be easily loaded into R using one line of code. However, what if we want to be able to analyze our own data sets using R? Typically, such data is in the form of a CSV file. Today, we are going to talk about how to load data from such a file into R.
The Data
The data we will be using as an example in this lab is the same data we will use for Lab 8. The Austin Animal Shelter in Austin, Texas, is the largest no-kill shelter in the United States. The organization cares for dogs, cats, and other animals in need, and each year, this work results in thousands of adoptions. Today, we will be working with random sample of 10,000 animals adopted from the Shelter in 2016 or 2017. Our task is to determine the average number of number of days an animal tends to spend in the Shelter prior to being adopted.
The data citation is provided below, but do not download the data from the link in the citation.
Data Citation
Data: Austin Animal Center Shelter Outcomes, Version 1. City of Austin Open Data Initiative. Retrieved January 10, 2022 from https://www.kaggle.com/aaronschlegel/austin-animal-center-shelter-intakes-and-outcomes.
Getting the Data
The first thing we need to do is get the data. You can download the file from here.
Create a folder on your computer called STA111, and store this csv file inside the folder.
Moving the data into RStudio
There are two ways that you can choose to access RStudio - on your own computer, or on RStudio Cloud. There are instructions below for each of these two options.
Option 1: For Folks Using RStudio on their Laptops
- Step 1 Open RStudio.
- Step 2 Look at the upper right hand panel of your screen.
- Step 3 Find “Import Dataset” or “Import” and click on it.
- Step 4 Choose “Text (base)” or “From CSV” (it will depend on your computer).
- Step 5 Find your data (AdoptedSamp.csv) in the list that comes up. Choose it!
The above 5 steps will allow you to load the data into RStudio. However, if you are using Markdown, you need a few more steps.
-
Step 6 Now, look the bottom right hand panel of your
screen. You should see a line of code with something like
train <- read.csv(“AdoptedSamp.csv”)
ortrain <- read_csv(“AdoptedSamp.csv”)`
. - Step 7 Copy that ENTIRE line of code.
- Step 8 Open a Markdown file.
- Step 9 Insert a code chunk (Look to the upper right corner of the file and find the little green C icon. Find “R” on the drop down menu, and click it!)
- Step 10 You will notice a gray box appears on your Markdown file. This is called a chunk, and is basically a spot where we can type code.
- Step 11 Paste the line from Step 7 into this gray code chunk, and press the green arrow (the play button).
And you are ready to go!! You now have the data loaded and you can work with it!
Option 2: For Folks Using RStudio Cloud
- Step 1 Open RStudio Cloud.
- Step 2 Look at the lower right hand quadrant of your RStudio screen./li>
- Step 3 At the top of that square, look for “Upload”.
- Step 4 Click Upload, and then browse for where you have stored the AdoptedSamp.csv data on your computer.
- Step 5 Once you have found the file, hit “Import”. This moves the data into RStudio Cloud, but you can’t work with it yet.
- Step 6 Now, look at the upper right hand panel of your screen.
- Step 7 Find “Import Dataset” and click on it.
- Step 8 - Choose “Text (base)” or “From CSV” (it will depend on your computer).
- Step 9 Find the data you want (AdoptedSamp.csv) in the list that comes up. Choose it!
-
Step 10 Now, look the bottom right hand panel of your
screen. You should see a line of code with something like
train <- read.csv(“AdoptedSamp.csv”)
ortrain <- read_csv(“AdoptedSamp.csv”)`
. - Step 11 Copy that ENTIRE line of code.
- Step 12 Open a Markdown file.
- Step 13 Insert a code chunk (Look to the upper right corner of the file and find the little green C icon. Find “R” on the drop down menu, and click it!)
- Step 14 You will notice a gray box appears on your Markdown file. This is called a chunk, and is basically a spot where we can type code.
- Step 15 Paste the line from Step 11 into this gray code chunk, and press the green arrow (the play button).