X hits on this document

57 views

0 shares

0 downloads

0 comments

12 / 23

10

AN INTRODUCTION TO R

R> csvForbes2000 <- read.table("Forbes2000.csv",

  • +

    header = TRUE, sep = ",", row.names = 1,

    • +

      colClasses = c("character", "integer", "character",

    • +

      "factor", "factor", "numeric", "numeric", "numeric",

    • +

      "numeric"))

R> class(csvForbes2000[,"name"])

[1] "character"

and check if this object is identical with our previous Forbes 2000 list object R> all.equal(csvForbes2000, Forbes2000)

[1] "Component \"name\": 23 string mismatches"

The argument colClasses expects a character vector of length equal to the number of columns in the file. Such a vector can be supplied by the c function that combines the objects given in the parameter list into a vector

R> classes <- c("character",

"integer",

"character",

"factor",

+

"factor",

"numeric",

"numeric",

"numeric",

"numeric")

R> length(classes)

[1] 9

R> class(classes)

[1] "character"

An R interface to the open data base connectivity standard (ODBC) is available in package RODBC and its functionality can be used to assess Excel and Access files directly:

R> library("RODBC") R> cnct <- odbcConnectExcel("Forbes2000.xls") R> sqlQuery(cnct, "select * from \"Forbes2000\\$\"")

The function odbcConnectExcel opens a connection to the specified Excel or Access file which can be used to send SQL queries to the data base engine and retrieve the results of the query.

Files in SPSS format are read in a way similar to reading comma separated files, using the function read.spss from package foreign (which comes with the base distribution).

Exporting data from R is now rather straightforward. A comma separated file readable by Excel can be constructed from a data.frame object via

R > w r i t e . t a b l e ( F o r b e s 2 0 0 0 , f i l e = " F o r b e s 2 0 0 0 . c s v " , s e p = " , " ,

  • +

    col.names = NA)

The function write.csv is one alternative and the functionality implemented in the RODBC package can be used to write data directly into Excel spread- sheets as well.

Alternatively, when data should be saved for later processing in R only, R objects of arbitrary kind can be stored into an external binary file via

R> save(Forbes2000, file = "Forbes2000.rda")

Document info
Document views57
Page views57
Page last viewedFri Dec 09 22:36:10 UTC 2016
Pages23
Paragraphs684
Words5996

Comments