In this post, we are going to talk about importing common data format that we often encounter, such as Excel and Text data. Most of the data are saved in MS Excel, and the best way to import this is to save this 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters