11.07.2015 Views

Preface to First Edition - lib

Preface to First Edition - lib

Preface to First Edition - lib

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

BASIC DATA MANIPULATION 11The function odbcConnectExcel opens a connection <strong>to</strong> the specified Excel orAccess file which can be used <strong>to</strong> send SQL queries <strong>to</strong> the data base engine andretrieve the results of the query.Files in SPSS format are read in a way similar <strong>to</strong> reading comma separatedfiles, using the function read.spss from package foreign (which comes withthe base distribution).Exporting data from R is now rather straightforward. A comma separatedfile readable by Excel can be constructed from a data.frame object viaR> write.table(Forbes2000, file = "Forbes2000.csv", sep = ",",+ col.names = NA)The function write.csv is one alternative and the functionality implementedin the RODBC package can be used <strong>to</strong> write data directly in<strong>to</strong> Excel spreadsheetsas well.Alternatively, when data should be saved for later processing in R only, Robjects of arbitrary kind can be s<strong>to</strong>red in<strong>to</strong> an external binary file viaR> save(Forbes2000, file = "Forbes2000.rda")where the extension .rda is standard. We can get the file names of all fileswith extension .rda from the working direc<strong>to</strong>ryR> list.files(pattern = "\\.rda")[1] "Forbes2000.rda"and we can load the contents of the file in<strong>to</strong> R byR> load("Forbes2000.rda")1.6 Basic Data ManipulationThe examples shown in the previous section have illustrated the importance ofdata.frames for s<strong>to</strong>ring and handling tabular data in R. Internally, a data.frameis a list of vec<strong>to</strong>rs of a common length n, the number of rows of the table. Eachof those vec<strong>to</strong>rs represents the measurements of one variable and we have seenthat we can access such a variable by its name, for example the names of thecompaniesR> companies companies[1][1] "Citigroup"and the <strong>to</strong>p three companies can be extracted utilising an integer vec<strong>to</strong>r ofthe numbers one <strong>to</strong> three:R> 1:3© 2010 by Taylor and Francis Group, LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!