Import Data

 Importing data into R is fairly simple. The data can be imported from selected files like excel, txt, SPSS, SAS and STATA. 

 The following simple command will help you to import data from text file. 

 sudheesh<-read.table("C:\\Documents and Settings\\Administrator\\Desktop\\mydata.txt", header=T) 

 or 

 sudheesh<-read.table("C:/Documents and Settings/Administrator/Desktop/mydata.txt", header=T) 

 Please make sure that the path is written carefully and note that we are using double slash instead of single. Otherwise the  path should be  given as in Linex. 

 To import from excel it is better to covert the excel file in to comma delimited format and then import from it using the following  command 

 sudheesh<-read.csv("C:/Documents and Settings/Administrator/Desktop/mydata.csv", header=T) 

 If we want to export from file like SPSS we have to specify the library as the command will not work in base package. The  particular package can be installed in your pc using the command 

 install.packages("package name") 

 Then specify the library. For Stata and Systat, use the foreign package. For SPSS and SAS use the Hmisc package 

 From SPSS 

 library(Hmisc)
 sudheesh<- spss.get("C:/Documents and Settings/Administrator/Desktop/mydata.por", use.value.labels=TRUE) 

 Note that we have to save SPSS data set in transport format before using the above command. The ast option converts value labels to R factors

 From SAS

 library(Hmisc)
 sudheesh <- sasxport.get(C:/Documents and Settings/Administrator/Desktop/mydata.dta") 

 Here also we want to save SAS dataset in transport format. Sorry I don’t know SAS so I did not work out it.   

 From STATA 

 library(foreign)
 sudheesh <- read.dta("C:/Documents and Settings/Administrator/Desktop/mydata.dta")
 

 From SYSTAT 

 library(foreign)
 sudheesh <- read.systat("C:/Documents and Settings/Administrator/Desktop/mydata.dta")

 

 back to notes