The following is a script file containing all R code of all sections in this slide set.

R Internal Data Set Format

data()
data(package="DMwR") # data sets from a specific package
data(package = .packages(all.available = TRUE)) # all packages
data(iris)
head(iris)
dat <- data.frame(x=rnorm(10),y=rnorm(10))
save(dat,file="exp.RData")
rm(dat)           # remove dat from the computer memory
dat               # confirming that it was deleted
load("exp.RData") # load it back from the file
head(dat)         # here it is again!

Data Sets in Text Files

library(readr)
dat <- read_csv("x.csv",col_types = "cci")
dat
dat <- read_csv2("y.csv", col_types = "ccd")
dat
dat <- read_table2("z.txt",na="???",col_types = "ccic")
dat 

Importing from Spreadsheets

dat <- read.table("clipboard",header=TRUE)
dat
library(readxl)
fc <- "c:\\Documents and Settings\\xpto\\My Documents\\calc.xls"
dat <- read_excel(fc,sheet="MayValues")
dat2 <- read_excel(fc,sheet=2)
dat <- read_excel(fc,sheet="MayValues", range="C2:F24")

Connecting with Data Bases

library(DBI)
library(RMySQL)
drv <- dbDriver("MySQL")  # Loading the MySQL driver
con <- dbConnect(drv,dbname="transDB",  # connecting to the DBMS
                 username="myuser",password="mypasswd",
                 host="localhost")

# getting the results of a query as a data frame
data <- dbGetQuery(con,"SELECT * FROM clients") 

dbDisconnect(con)  # closing up stuff
dbUnloadDriver(drv)
library(DBI)
library(RMySQL)
drv <- dbDriver("MySQL")  # Loading the MySQL driver
con <- dbConnect(drv,dbname="transDB",  # connecting to the DBMS
                 username="myuser",password="mypasswd",
                 host="localhost")

res  <- dbSendQuery(con,"SELECT * FROM transactions") 
while (!dbHasCompleted(res)) {
    # get the next 50 records on a data frame
    someData <- fetch(res, n = 50)  
    # call some function that handles the current chunck
    process(someData)  
}
dbClearResult(res) # clear the results set

dbDisconnect(con)  # closing up stuff
dbUnloadDriver(drv)