The following information will provide a basic introduction to importing and exporting data into and out of R. For more details, please refer to the R Data Import/Export Manual that is found on the R Project website.
Importing data from an external ascii file
Several functions can be used to read data from an external ascii file (text file) into R and store it into an R object. However, if data is stored in some other type of format (e.g. an Excel spreadsheet), you may want to use the application in which the data is stored in (e.g. Excel) to export the data to a text file.
The function, scan, will read data into a vector or list from a console or ascii file. To see how this is done, first create a file, "file1.txt", in the directory in which you run R. In Windows, this means that you must change your working directory to the directory in which "file1.txt" is in. You can do this by simply left clicking on the File menu and selecting Change dir... .
For instance, suppose the text file, "file1.txt", contains the following information on the weight of ten subjects.
195 143 210 105 177 154 123 233 118 166
To scan the data in "file1.txt" to the vector, x, type:
> x<-scan("file1.txt")
> x
Type
> help(scan)
for more information on the available options.
The function, read.table, will read an ascii file (text file) in table format and create a data frame from it. Table format means that the lines in the file correspond to cases and the columns in the file correspond to variables. A header may (or may not) be included in the file indicating the name of the variable in each column. A data frame is used with most R modeling software and is similar in nature to matrices and lists.
We can read the data from the file, "file1.txt" into a data frame, x, using the function, read.table. To see how this is done, first create a file, "file1.txt", in the directory in which you run R. For instance, suppose the text file, "file1.txt", contains the following information on the weight and age of ten subjects.
wt age 195 44 143 32 210 56 105 24 177 37 154 32 123 54 233 28 118 30 166 47
To scan the data in "file1.txt" to the vector, x, type:
> x
<- read.table("file1.txt",header=TRUE) > x
Type
> help(read.table)
for more information on the available options.
Importing data from a text file on a webpage
It is likely that instead of having an ascii file saved in the directory in which you are using R, the file to be imported may exist on a webpage. Using any of the commands for importing data given previously (i.e. scan, read.table), the file can also be imported by specifying the URL as the filename. For example, you can view the data for the weight and age of ten subjects given previously using the URL, "http://www.math.uwaterloo.ca/~dbabinea/webpage/R.tut/R.tut.win/file1".
To import this dataset to R using the URL as the filename, just type:
> x <- read.table("http://www.math.uwaterloo.ca/~dbabinea/webpage/R.tut/R.tut.win/file1",header=TRUE)
Exporting data
Several functions can be used to write an R object to an external ascii file (text file).
The function, write, writes out a matrix or vector in a specified number of columns. To see how this is done to the matrix, x, first put the matrix, x, into R by typing:
> x1 <- 1:10
> x2 <- 11:20
> x3 <- 21:30
> x <- cbind (x1,x2,x3)
To write the matrix, x, so that each column appears as a line in the text file, type:
> write(x,file="file1.txt",ncolumns=10)
Alternatively, if you want each row in the matrix, x, to appear as a line in the text file, type:
> write(t(x),file="file2.txt",ncolumns=3)
The function, write.table, will write a data frame (after converting it to a data frame if it isn't already one) to an external ascii file (text file). Entries in each line(row) are separated by the value of 'sep;. The default value for 'sep' is a space. This is done to the object, x2, by typing:
> x1<-1:10
> x2<-11:20
> x3<-21:30
> x<-cbind(x1,x2,x3)
> x2<-data.frame(x)
> write.table(x2,"file3.txt",quote=FALSE)
In this case, the object is written to a file, file3.txt, which sould now be in the firectory in which you run R.
Type
> help(write.table)
for more information on the available options. Note that the function, write.table, can be slow for data frames that have hundreds of columns. The function, write.matrix, in the package, 'MASS', is a more efficient way of dealing with this problem if the object being exported can be represented as a numeric matrix.