Lesson 8: Importing Data

Overview Section

So far in our lessons, we've mostly entered our data direclty into R or used built-in data sets that are part of R. This won't take us very far for real work; to really use R for data analysis we must get data into R! We'll use Chapter 8 in Essential R.


Upon completion of this lesson, you should be able to:

  • Prepare a text file (.txt or .csv) for import into R
  • Import the data using the function read.table()
  • Recognize and fix the most common problems that cause errors when importing data

Data and R Code Files Section

The R code file and data files for this lesson can be found on the Essential R - Notes on learning R page.

The following data files should be in the "Data" folder in your "Essential R" folder; or you can save them to your working directory.

W101-2010.xls (Excel file) and W101-2010.csv (text file made from the Excel file). The example text files are: Ex1.txt, Ex2.txt, Ex3.txt, and Ex3.csv. The data file required for Exercise 2 is here: StatesData.xls.

Note! The videos don't cover the use of the "Import Dataset" dialog in the Environment browser pane in RStudio, but it is discussed in Chapter 8 of EssentialR. Do note that the current version of RStudio have updated the import data dialog to use the package readr. This replaces read.csv() with read_csv(). This will read in your data but may convert some of the variables differently than read.csv() would do. (read_csv() will create a "tibble", which is subtly different from a data.frame). My suggestion to avoid the confusion is to edit the code that rstudio wrote in your console - replace read_csv() with read.csv and rerun the code - you should be fine.