One of the most important features we need to be able to do in R is import existing data, whether it be .txt files, .csv files, or even .xls (Excel files). If we can’t import data into R, then we can’t do anything. Okay let’s get started.
The spirit of this blog is that whatever I do here, should also work for someone working from home. Thus, we all need to work from the same text file, so we have to build a simple text file together to make this whole process work. Since this isn’t R code, I’ll just break it down into simple steps that are easy to follow.
Step 1: open notepad
Step 2: enter data as I have shown below (no spaces, use only commas)
Step 3: save the file as ‘rain.txt’ on your Desktop
Okay, great now it’s time to get to work in R importing this data. We have two options, importing using the R Studio environment (the easy way), or importing using standard R functions.
The Easy way (Import through R Studio)
Step 1: Click the ‘Import Dataset’ button, then click ‘From Local File’
Step 2: Navigate to the ‘rain.txt’ file located on your Desktop and click ‘open’. The next dialog box we get shows the values contained within our file, and different importing options. A few things to notice, ‘Name’ at the top has been set to “rain”, which will become the variable our data is stored as in R. The ‘Heading’ radio button has already been moved to ‘yes’ because R Studio has recognized our column headers (month, rain_mm, flow_cms). Additionally, the ‘Separator’ has been adjusted to ‘comma’ as we have made a comma delimited text file. All you have to do is just click ‘Import’.
Step 3: R Studio automatically opens the ‘rain’ dataset as a table in a new tab. R Studio also provides the snippet of code it used to import the data, which is great! You can copy that code and paste it into your R script file for future use.
That’s it! You’re a pro at importing data using R Studio.
The Hard way (Import using R functions)
There’s lots of functions that can be used to import data into R: read.table, read.csv, read.csv2, read.delim, read.delim2 (among others). We’ll use read.table in this example.
To understand how this function works, let’s open up the R help by typing ?read.table.
# Get R help ?read.table
That should open up a help file through your web browser, or in the lower right ‘Help’ menu if you’re using R Studio. The main pieces of this function we need to set are ‘file’, ‘header’, and ‘sep’.
-The ‘file’ piece is the file name and file path we want to import.
-The ‘header’ piece is set to TRUE or FALSE based on whether or not there is a header within the file.
-The ‘sep’ piece describes the separator used within the file (in our case, a comma)
So, here’s how our code should look:
# Import the data rain<-read.table("C:/Users/YOUR-NAME/Desktop/rain.txt", header = TRUE, sep = ",")
Two things of note in the code above.
-The part of the path “YOUR-NAME” is based on your computer login settings. It might be something like ‘Tim’, ‘Jane’, ‘PeterC’, etc.
-I have defined this data as “rain” in R, using the rain <- bit of code. Here’s how the data looks in R.
# The rain data # month rain_mm flow_cms #1 1 128 15000 #2 2 98 12000 #3 3 92 11000 #4 4 77 9800 #5 5 68 7600 #6 6 63 5800 #7 7 76 5500 #8 8 81 5700 #9 9 84 6200 #10 10 122 9500 #11 11 117 15000 #12 12 125 1700
That’s it! You’re a pro at importing data into R via the hard way! Happy coding!