Mike Spencer
2016-06-15
Preamble
install.packages("RSQLite")
library(RSQLite)
Connection
db = dbConnect(SQLite(), "~/Cloud/Michael/Uni_temp/SSGB/SSGB.sqlite")
You're now good to go!
# Available tables
dbListTables(db)
# Columns in a table
dbListFields(db, "SSGB_stations")
# Read all columns from obs table from Aviemore
head(dbGetQuery(db, "SELECT * FROM SSGB_obs WHERE Station='Aviemore'"))
head(dbGetQuery(db, "SELECT * FROM SSGB_obs WHERE Station='Aviemore' AND Snowline!='m'"))
df = dbGetQuery(db, "SELECT * FROM SSGB_obs WHERE Station='Aviemore' AND Snowline!='m'")
# lapply multiple stations
st = c("Aviemore", "Eskdalemuir", "Dalwhinnie")
dl = lapply(seq_along(st), function(i){
dbGetQuery(db, paste0("SELECT HydroYear, Station, SnowlineElev, COUNT(SnowlineElev) AS Days FROM SSGB_obs WHERE Station='", st[i], "' GROUP BY HydroYear, Station, SnowlineElev"))
})
# Temp tables
dbSendQuery(db, "
CREATE TEMP TABLE step AS
SELECT * FROM SSGB_obs
WHERE strftime('%m', Date) BETWEEN '11' AND '12'
AND SnowlineElev !='m'
OR strftime('%m', Date) BETWEEN '01' AND '04'
AND SnowlineElev !='m'
")
# Create a new table
dbSendQuery(db,
"CREATE TABLE fielddata
(HydroYear INT,
Date DATETIME,
Station TEXT,
Snowline TEXT,
SnowlineElev TEXT,
PRIMARY KEY(Date, Station))
")
# Write to it
dbWriteTable(conn=db, name="SSGB_obs", new.data, append=T, row.names=F)
Parallel is available in R core
library(parallel)
mclapply()
# parameters
install.packages("TunePareto")
library(TunePareto)
params = list(
a = 1:10,
b = seq(0.1, 1, by=0.1))
# How many iterations?
prod(sapply(params, length))
# Get all parameter combinations
params = allCombinations(params)
params = lapply(params, function(i){
do.call("cbind.data.frame", i)
})
params = do.call("rbind.data.frame", params)
# Single core
ptm = proc.time()
dl = lapply(1:nrow(params), function(i){
rnorm(n=10, mean=params[i, 1], sd=params[i, 2])
})
proc.time() - ptm
# Multi core
ptm = proc.time()
dl = mclapply(1:nrow(params), mc.cores=2, function(i){
rnorm(n=10, mean=params[i, 1], sd=params[i, 2])
})
proc.time() - ptm
Packages used:
Twitter: @mikerspencer
LinkedIn: https://www.linkedin.com/in/mikerspencer
Email: spencer.mike.r@gmail.com
Website: http://mikerspencer.com