R tutorial: Final thoughts on importing data with R

1,751 views · Published 10 November 2016 · 2:10 · Indexed 6 October 2026

Channel: DataCamp · 2016 · Education

Watch on YouTube

Learn more about importing data in R: https://www.datacamp.com/courses/importing-data-in-r-part-1

There's something I haven't told you yet. When I said that read.table() was utils' main importing functions, I was serious. Actually, read.csv() and read.delim(), the functions to import comma-separated values and tab-delimited files, are so-called wrapper functions around read.table(). They call read.table() behind the scenes, but with different default arguments to match the specific formats.

For read.csv(), the default for header is TRUE and for sep is a comma, so you don't have to manually specify these anymore. This means that this read.table() call to import the CSV version of states, is exactly the same as this read.csv() call. Shorter and easier to read, if you ask me. Likewise, read.delim() sets the header and sep argument, among some others. This call to import the tab-delimited version of states, is exactly the same as this read.delim() call.

If you have a look at the documentation of read.table(), you'll see that there are two more functions in there that we haven't discussed yet. read.csv2() and read.delim2(). These functions exist to deal with regional differences in representing numbers. Have a look at this csv file, states_aye.csv, typical for the US and Great Brittain, and its counterpart, states_nay.csv.

You'll notice that the states_nay use commas for decimal points, as opposed to the dot for states_aye.csv. This means that they can't use the comma as the field-delimiter anymore, they need a semicolon.

That's why the read.csv2() and read.delim2() functions exist. Can you spot the difference in default arguments again?

Let's try to import the states_nay.csv file with the basic read.csv() function.

R gives a result, but it clearly is not the result we want. It's a dataset with 5 observations but a single variable.

If we try again with read.csv2(), it works perfectly this time!

These were just some side notes to wrap up on this chapter. By now, you now how to import comma-separated, tab-delimited and even more exotic data formats. But there's much more to learn! I hope to see you in the next chapter to learn about the powerful readr and data.table packages!

More from this channel