Importing Data to R

[This article was first published on Analysis with R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

There are number of ways in importing data into R, and several formats are available,
  • From Excel to R
  • From SPSS to R
  • From Stata to R, and more here
In this post, I’m going to talk about importing common data format that we often encounter, such as Excel, or Text data. Most of the data are saved in MS Excel, and the best way to import this is to save this as in CSV format, below is the procedure:
  • Open your Excel data
  • Go to File > Save As or press Ctrl+Shift+S
  • Name this with anything you want, say Data. Then before clicking Save, make sure to change the File Format to Comma Delimited Text and better set the directory to My Documents folder, for Windows.
  • When saved, this file will have a name Data.csv.
Now open R, and run the following

The argument header = TRUE tells R that the first row of the data are the labels of every column. If set to FALSE, means the first row of the data are not the labels, but are considered as data points.

Now in some cases, data are saved in Text (.txt) format. And to import this, we use the read.table function. Consider the data below, and say this is saved as Data1.txt in My Documents folder (for Windows).

To import this to R, simply run

Or apply read.table directly to the data

There are times, however, when the Text data are saved in the internet, here is an example. To import this to R, of course, make sure of the internet connection first. Next, copy the URL of the data and assign this to any variable name, then apply read.table. Try the codes below,

Please comment below if you have any problems.

To leave a comment for the author, please follow the link and comment on their blog: Analysis with R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)