Data Import

Importing data with R

R can import different sorts of files from the working directory or from outside sources.

Tab delimited or table file

If your file is stored as Tab delimited file or table, say mydata.txt, to import it into the R-environment, the command "read.table" will be sufficient.

>read.table("mydata.txt")

CSV file

If your file is stored as CSV (Comma Separated Values) file , say mydata.csv, to import it into the R-environment, the command "read.csv" will be sufficient.

>read.csv("mydata.csv")

Excel file

If your file is stored an Excel file, say mydata.xls , to import it into the R-environment, there are couple of steps that you take. Step 1 can be done only once.



>install.packages("gtools"); install.packages("gdata")    # Install the packages "gtools" and "gdata" from the the command
                                                           line.
You will have to do this only once.
> library(gdata)                 # load the package "gdata" into the R workspace since it isn't in the R core library
> read.xls("mydata.xls")         # open the Excel file "mydata.xls" into the R workspace.


Minitab Worksheet file

If your file is stored as a Minitab portable worksheet, say mydata.mtp, to import it into your R-workspace, you need to install the package "foreign". 

>install.packages("foreign")         # installing the package "foreign". You will have to do this only once.
>library(foreign)                    # load the package "foreign" into the R workspace since it isn't in the R core library
>read.mpt("mydata.mtp")              # open the Minitap file "mydata.mtp" into the R workspace.


SPSS

If your file is stored in SPSS (Statistical Package for Social Sciences) format,  say mydata.spss, to import it into  your R-workspace, you need to install the package "foreign".

If you've already install this packages, not need to redo it. If not, install it first.

>install.packages("foreign")                      # installing the package "foreign". You will have to do this only once.
>library(foreign)                                 # load the package "foreign" into the R workspace since it isn't in the R core library
>read.spss("mydata.spss",to.data.frame=TRUE)      # open the SPSS file "mydata.spss" into the R workspace as a data frame.

SAS

If your file is stored in SAS format,  say mydata.ssd, to import it into  your R-workspace, you need to install the package "foreign".
If you've already install this packages, not need to redo it. If not, install it first.

>install.packages("foreign")              # installing the package "foreign". You will have to do this only once.
>library(foreign)                         # load the package "foreign" into the R workspace since it isn't in the R core library>read.ssd("mydata.ssd")


Other types:

R can import many other type of files. The command help(package=foreign) will give you more details.


From the internet

Data can be imported directly from the internet, you know the type: say we have the data file, ACTH1.txt stored at the web-address http://ramanujan.math.trinity.edu/ekwessi/misc/ACTH1.txt 

To import it into R

>read.table(file="http://ramanujan.math.trinity.edu/ekwessi/misc/ACTHJ1.txt",header=TRUE, na.strings="0", fill=TRUE)