R I/O

From Wiki
Revision as of 23:23, 4 February 2011 by Scott (talk | contribs) (Connect to a database)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Save and load native R-format files

save(top.5.salaries,file="~/top.5.salaries.RData")
load("~/top.5.salaries")

Read data from a file

Sample data file:

name.last,name.first,team position,salary
"Manning","Peyton","Colts","QB",18700000
"Brady","Tom","Patriots","QB",14626720
"Pepper","Julius","Panthers","DE",14137500
"Palmer","Carson","Bengals","QB",13980000
"Manning","Eli","Giants","QB",12916666

Read:

top.5.salaries = read.table("top.5.salaries.csv", header=TRUE, sep=",", quote="\"")

Options:

  • header: default is no header row
  • sep: whitespace is default separator
  • quote: none is default
  • row.names: A character vector containing row names for the returned data frame
  • nrows: number of rows to read (default is all)
  • strip.white: default is to not strip white space
  • blank.lines.skip: default is to skip blank lines
  • comment.char: ignores lines that begin with # by default

Alternate commands: read.csv assumes a header row, comma-separated, quoted read.delim assumes a header row, tab-delimited, quoted read.fwf works for fixed-width files readLines gives you one string per line scan lets you read data directly into a pre-defined data structure

Write data to a file

write.table(top.5.salaries, "top.5.salaries.txt")

Also write.csv and write.delim.

Read data from a web page

sp500 = read.csv(paste("http://ichart.finance.yahoo.com/table.csv?",
    "s=%5EGSPC&a=03&b=1&c=1999&d=03&e=1&f=2009&g=m&ignore=.csv"))

Connect to a database

Use the RMySQL package to connect to MySQL: http://rss.acs.unt.edu/Rdoc/library/RMySQL/html/RMySQL-package.html

Install:

install.packages("RMySQL")

Connect:

library(RMySQL)
connection = dbConnect(MySQL(), host="localhost", dbname="sp5", user="barney", password="XXXXX")

Import and export data.frames:

d = dbReadTable(con, "WL")
dbWriteTable(con, "WL2", a.data.frame)         ## table from a data.frame
dbWriteTable(con, "test2", "~/data/test2.csv") ## table from a file

Run an arbitrary SQL statement and extract all its output (returns a data.frame):

dbGetQuery(con, "select count(*) from a\_table")
dbGetQuery(con, "select * from a\_table")

Disconnect

dbDisconnect(connection)