1. read.delim & read.table
In the previous video you learned about read-dot-csv, which is an R function that specifically exists for importing CSV data. However, flat file data doesn't come as comma separated values alone.
2. Tab-delimited file
Another common format of flat file data is the tab-delimited file, like this states-dot-txt, with the same data as before:
To import it, we'll need read-dot-delim this time. As usual, you simply specify the path to the file. Works like a charm!
If your data comes in this typical comma separated or tab-delimited format, your life is easy and importing the data is a walk in the park. If it's not, you'll have to do some more customization work.
3. Exotic file format
Say we have the same states data again, but this time, the values are separated by a forward slash instead of commas or tabs, in a file states2.txt. How to go about this? read-dot-csv and read-dot-delim won't do you much good in this case.
4. read.table()
Here, you'll want to use read-dot-table. It's the main importing function in the utils package, allowing you to read in any file in table format and create a data frame from it.
The number of arguments you can specify for this function is huge, so I won't go through each and every one of these arguments. Instead, let's have a look at the read-dot-table call that imports states2.txt and try to understand what happens.
As usual, the first argument is the path to the file you want to import.
The header argument is something we haven't seen before. If you set this to TRUE, you tell R that the first row of the text file contains the variable names, which is the case here. read-dot-table sets this argument to FALSE by default, which would mean that the first row is always read as an observation, even if it's a row of variable names.
Next, sep is the argument that specifies how fields in a record are separated. For our file here, the field separator is a forward slash, like this.
The result looks just as we'd want, nice!
Apart from the arguments we discussed here, there are also ways to specify column names and column types, among others.
5. Let's practice!
Get some practice on them in the exercises.