Tested on R versions 3.0.X through 3.3.1
Last update: 15 Auguat 2016
Data are exported from R in three basic formats:
Data from Exercise #6 (objects f1, m1, m2 ,m3, m4, t1, and w1) were saved as mod3data.RData
. Some of these objects will be needed, so load them first into your workspace.
# load objects from Exercise #6; should have saved as mod3data.RData
# REMEMBER your directory path will be different .. I'm a long-winded instructor
getwd() # in correct directory ?
## [1] "C:/Users/tce/Documents/words/classes/baseR_ALLversions/baseR-V2015.3/data/powerpoint_dat"
list.files(pattern = ".RData") # is it there ?
## [1] "mod3data.RData"
load("mod3data.RData") # load it
ls() # check workspace; objects present ?
## [1] "f1" "m1" "m2" "m3" "m4" "t1" "w1"
If the objects are not there, or you did not save an .RData
from Exercise #6, you will need to return to Module 3.4, Exercise #6, and re-import the data before proceeding further.
Exporting whitespace-, tab-delimited, and comma-separated-value files uses the write.FileType(DataObject, "PATH/FileName.FileExt")
call. By default write.FileType()
carries over the column names to the export file.
Basic options for FileType are .csv
and .table
. The FileExt for .csv files on FileName must be .csv
; table is usually as .txt
although .csv
works equally well.
Two important options are: row.names = F
to avoid a column of observation numbers being exported to the output file, and option NA = "Symbol"
to avoid R NA
’s being written out as literal NA. The symbol for blanks is ""
.
# ensure you're in the proper directory for your output; change if necessary
# NOT RUN; assume f2 an existing data object
write.csv(f2, "filename.csv", row.names = F) # export f2 as csv
write.table(f2, "filename.txt", sep = "\t", na = "") # export f2 .txt, tab-delim
write.table(f2, "filename.txt", sep = "", na = "") # export f2 as.txt, whitespace
Export for .dbf
files is similar, but requires the package foreign be installed and loaded.
# NOT RUN; export for .dbf, assume f2 an existing data object
library(foreign) # load package foreign
write.dbf(f2, "filename.dbf") # export f2 as dbf
Exports to other statistical package formats can be cumbersome. The simplest option is to export to a .csv file and import that into the relevant statistical package. See packages foreign, RODBC and gdata for other options.
The output from a R analysis often includes text co-mingled with numbers. A not uncommon desire is to export the object infomration as text rather than as a R data object (i.e, .RData
format) ot in a file structure like .csv.
One method for exporting simple text uses sink(FileName.txt, type = output)
. Two important options are append = T
and split = T
. The first continues to append text to an open file, and is very useful in a loop or custom functions (see Modules 4.8 and 4.9, respectively). The second shows output on the console as it is being written. The output text file must be closed at the end of the oepration using sink()
.
# NOT RUN; export text using sink(); no options
sink(file = "filename2.txt", type = "output") # file for output text
# some sort of export text from R will be called here
sink() # MUST close file w/this call
# NOT RUN; export text where each new operation is appended
sink(file = "filename2.txt", type = "output", append = T) # file for output text
# some sort of export text from R will be called here
sink() # MUST close file w/this call
Basic calls related to data output are:
write.csv()
=> Output to .csv filewrite.table()
=> Output to .table format; typically a .txt filewrite.dbf()
=> Output to .dbf filesink()
=> Open new file fortext output; used toclose file as wellData for this exercise grazing_impacts.RData are in: ../baseR-V2016.2/data/exercise_dat.
The grazing data object grazing_impacts.RData was corrected in Exercise #9. Although it was saved as a .RData
object, there is desire to have a corrected version for EXCEL as well.