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,Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
- 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.
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
MyRData <- read.csv("Data.csv", header = TRUE) |
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
College Prof Asct.Prof Asst.Prof Ins | |
CASS 30 21 23 19 | |
CBAA 6 6 8 2 | |
CED 15 12 13 14 | |
COE 16 10 8 9 | |
CON 0 1 9 12 | |
CSM 45 36 28 15 | |
IDS 6 6 10 9 | |
SCS 4 10 11 10 | |
SET 23 11 8 11 |
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
MyRData2 <- read.table("Data1.txt", header = TRUE) |
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
MyRData3<- read.table(header = TRUE, | |
text = "College Prof Asct.Prof Asst.Prof Ins | |
CASS 30 21 23 19 | |
CBAA 6 6 8 2 | |
CED 15 12 13 14 | |
COE 16 10 8 9 | |
CON 0 1 9 12 | |
CSM 45 36 28 15 | |
IDS 6 6 10 9 | |
SCS 4 10 11 10 | |
SET 23 11 8 11") |
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
library(RCurl) | |
web <- "https://raw.githubusercontent.com/alstat/Analysis-with-Programming/master/2013/R/How%20to%20Enter%20Your%20Data/Data.dat" | |
x <- getURL(web) | |
y <- read.table(text = x, header = TRUE) | |
# OUTPUT | |
id Code S3 S2 S1 S1.1 S2.1 S3.1 Weight.class | |
1 Slope1 6 4 2 NA NA NA NA | |
2 Slope2 16 8 4 NA NA NA NA | |
3 Slope3 30 16 8 NA NA NA NA | |
4 Flood 2 1 1 NA NA NA NA | |
5 Drainage 3 3 2 NA NA NA NA |
To leave a comment for the author, please follow the link and comment on their blog: Analysis with R.
R-bloggers.com 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.