# one possibility: type the data in
d <- data.frame(
  location= c("Quito", "Capehope", "Rome",  "Paris", "Lapland"),
  x=        c(0.0000,   0.2987,    0.4648,  0.5762,   0.8386),
  y=        c(51,       337,       279,     374,      722)
  )
print(d)
##   location      x   y
## 1    Quito 0.0000  51
## 2 Capehope 0.2987 337
## 3     Rome 0.4648 279
## 4    Paris 0.5762 374
## 5  Lapland 0.8386 722
rm(list=ls())
# some data comes with R, you don't want to think too much about this
summary(cars)
##      speed           dist    
##  Min.   : 4.0   Min.   :  2  
##  1st Qu.:12.0   1st Qu.: 26  
##  Median :15.0   Median : 36  
##  Mean   :15.4   Mean   : 43  
##  3rd Qu.:19.0   3rd Qu.: 56  
##  Max.   :25.0   Max.   :120
ls()
## character(0)
# data()
# help(data)
cars <- cars
ls()
## [1] "cars"
rm(list=ls())
# data in a package, a more orderly built-in
# Data Boscovich used to estimate the ellipticity of the earth.
# 1755
library('quantreg')
## Loading required package: SparseM
## 
## Attaching package: 'SparseM'
## 
## The following object is masked from 'package:base':
## 
##     backsolve
search()
##  [1] ".GlobalEnv"        "package:quantreg"  "package:SparseM"  
##  [4] "package:stats"     "package:graphics"  "package:grDevices"
##  [7] "package:utils"     "package:datasets"  "package:methods"  
## [10] "Autoloads"         "package:base"
# data(package='quantreg')
# help(Bosco)
data(Bosco)
ls()
## [1] "Bosco"
print(Bosco)
##               x   y
## Quito    0.0000  51
## Capehope 0.2987 337
## Rome     0.4648 279
## Paris    0.5762 374
## Lapland  0.8386 722
rm(list=ls())
# Reading from a TSV file
d <- read.table('Boscovich.tsv',header=TRUE,sep='\t',stringsAsFactors=FALSE)
print(d)
##   location      x   y
## 1    Quito 0.0000  51
## 2 Capehope 0.2987 337
## 3     Rome 0.4648 279
## 4    Paris 0.5762 374
## 5  Lapland 0.8386 722
rm(list=ls())
# save in a few forms for later use
# to clear: rm -rf Boscovich.Rds Boscovich.SQLite Boscovich.h2db*
d <- read.table('Boscovich.tsv',header=TRUE,sep='\t',stringsAsFactors=FALSE)
saveRDS(d,'Boscovich.Rds')
library('RSQLite')
## Loading required package: DBI
con <- dbConnect(SQLite(),'Boscovich.SQLite')
rs <- dbSendQuery(con,'CREATE TABLE Boscovich (location TEXT, x REAL, y REAL)')
dbClearResult(rs)
## [1] TRUE
rs <- dbSendPreparedQuery(con,'INSERT INTO Boscovich VALUES (?,?,?)',d)
dbClearResult(rs)
## [1] TRUE
dbDisconnect(con)
## [1] TRUE
options( java.parameters = "-Xmx2g" )
library('RJDBC')
## Loading required package: rJava
drv <- JDBC('org.h2.Driver','h2-1.3.176.jar',identifier.quote='"')
con <- dbConnect(drv,'jdbc:h2:Boscovich.h2db')
dbWriteTable(con,'Boscovich',d)
## [1] TRUE
dbDisconnect(con)
## [1] TRUE
rm(list=ls())
# Reading a native R save
d <- readRDS('Boscovich.Rds')
print(d)
##   location      x   y
## 1    Quito 0.0000  51
## 2 Capehope 0.2987 337
## 3     Rome 0.4648 279
## 4    Paris 0.5762 374
## 5  Lapland 0.8386 722
rm(list=ls())
# read directly from Microsoft Excel
library('gdata')
## gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
## 
## gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
## 
## Attaching package: 'gdata'
## 
## The following object is masked from 'package:stats':
## 
##     nobs
## 
## The following object is masked from 'package:utils':
## 
##     object.size
dA <- read.xls('Boscovich.xlsx',sheet=1,
               header=FALSE,stringsAsFactors=FALSE,
               as.is=TRUE,colClasses='character')
print(dA)
##                     V1       V2                V3
## 1       Boscovich data            sin^2(latitude)
## 2                      location                 x
## 3                         Quito                 0
## 4 check this one -&gt; Capehope 0.298700000000000
## 5                          Rome 0.464800000000000
## 6                         Paris 0.576200000000000
## 7                       Lapland 0.838600000000000
##                                   V4
## 1 arc-length of 1 degree of latitude
## 2                                  y
## 3                                 51
## 4                                337
## 5                                279
## 6                                374
## 7                                722
d <- dA[c(-1,-2),-1]
colnames(d) <- dA[2,-1]
for(cn in colnames(d)) {
  d[[cn]] <- type.convert(d[[cn]],as.is=TRUE)
}
print(d)
##   location      x   y
## 3    Quito 0.0000  51
## 4 Capehope 0.2987 337
## 5     Rome 0.4648 279
## 6    Paris 0.5762 374
## 7  Lapland 0.8386 722
rm(list=ls())
# read from SQLite
library('RSQLite')
con <- dbConnect(SQLite(),'Boscovich.SQLite')
rs <- dbSendQuery(con,'SELECT * FROM Boscovich')
d <- fetch(rs,-1)
dbClearResult(rs)
## [1] TRUE
dbDisconnect(con)
## [1] TRUE
print(d)
##   location      x   y
## 1    Quito 0.0000  51
## 2 Capehope 0.2987 337
## 3     Rome 0.4648 279
## 4    Paris 0.5762 374
## 5  Lapland 0.8386 722
rm(list=ls())
# read from a JDBC connection to an arbitrary database
options( java.parameters = "-Xmx2g" )
library('RJDBC')
drv <- JDBC('org.h2.Driver','h2-1.3.176.jar',identifier.quote='"')
con <- dbConnect(drv,'jdbc:h2:Boscovich.h2db')
d <- dbGetQuery(con,'SELECT * FROM Boscovich')
dbDisconnect(con)
## [1] TRUE
print(d)
##   LOCATION      X   Y
## 1    Quito 0.0000  51
## 2 Capehope 0.2987 337
## 3     Rome 0.4648 279
## 4    Paris 0.5762 374
## 5  Lapland 0.8386 722
rm(list=ls())
# treat a data frame as a SQL database table
options(gsubfn.engine = "R") # prevent a crashing attempt to start X11 on OSX
library('sqldf')
## Loading required package: gsubfn
## Loading required package: proto
d <- read.table('Boscovich.tsv',header=TRUE,sep='\t',stringsAsFactors=FALSE)
dj <- sqldf('
   SELECT
      d1.location location1,
      d2.location location2,
      d2.x-d1.x deltax,
      d2.y-d1.y deltay
   FROM
      d d1
   JOIN
      d d2
')
class(dj)
## [1] "data.frame"
print(dj)
##    location1 location2  deltax deltay
## 1      Quito     Quito  0.0000      0
## 2      Quito  Capehope  0.2987    286
## 3      Quito      Rome  0.4648    228
## 4      Quito     Paris  0.5762    323
## 5      Quito   Lapland  0.8386    671
## 6   Capehope     Quito -0.2987   -286
## 7   Capehope  Capehope  0.0000      0
## 8   Capehope      Rome  0.1661    -58
## 9   Capehope     Paris  0.2775     37
## 10  Capehope   Lapland  0.5399    385
## 11      Rome     Quito -0.4648   -228
## 12      Rome  Capehope -0.1661     58
## 13      Rome      Rome  0.0000      0
## 14      Rome     Paris  0.1114     95
## 15      Rome   Lapland  0.3738    443
## 16     Paris     Quito -0.5762   -323
## 17     Paris  Capehope -0.2775    -37
## 18     Paris      Rome -0.1114    -95
## 19     Paris     Paris  0.0000      0
## 20     Paris   Lapland  0.2624    348
## 21   Lapland     Quito -0.8386   -671
## 22   Lapland  Capehope -0.5399   -385
## 23   Lapland      Rome -0.3738   -443
## 24   Lapland     Paris -0.2624   -348
## 25   Lapland   Lapland  0.0000      0
rm(list=ls())