Skip to content

Instantly share code, notes, and snippets.

View oscarperpinan's full-sized avatar

Oscar Perpiñán Lamigueiro oscarperpinan

View GitHub Profile
library(data.table)
library(lattice)
## Create a toy example
dt <- data.table(A=rnorm(1e7, mean=0, sd=1))
## Bin the data using equal-width intervals
brks <- seq(-6, 6, length=10000)
## and := to avoid an additional copy
dt[,bin:=findInterval(A, brks)]
## Finally aggregate with mean by bin
library(data.table)
data(movies, package='ggplot2')
library(lattice)
movies <- data.table(movies)
nBins <- 500
## 2D Binning
movies[, c('ratBin', 'votesBin'):=list(floor(rating/nBins), floor(votes/nBins))]
library(sp)
library(ggmap)
library(RColorBrewer)
## Get data from OpenPV. Open this URL:
## https://openpv.nrel.gov/search?&state=CA&minsize=100&maxsize=30600&pagenum=1&nppage=25
## and export the results as a CSV file named "californiaOpenPV.csv"
caPV <- read.csv('californiaOpenPV.csv')
## Longitude and Latitude are the names of columns where the spatial information is stored.
## With the coordinates<- method caPV is now an SpatialPointsDataFrame
library(raster)
library(rasterVis)
library(maptools) ## for readShapeLines
library(colorspace) ## for terrain_hcl
## Get data from GADM and DIVA-GIS
setwd(tempdir())
download.file('http://www.gadm.org/data/shp/ETH_adm.zip', 'ETH_adm.zip')
unzip('ETH_adm.zip', exdir='.')
download.file('http://www.diva-gis.org/data/msk_alt/ETH_msk_alt.zip', 'ETH_msk_alt.zip')
library(solaR)
## Monthly averages of daily values (Wh/m2)
G0dm <- c(2.766,3.491,4.494,5.912,6.989,7.742,7.919,7.027,5.369,3.562,2.814,2.179)*1000;
Ta <- c(10, 14.1, 15.6, 17.2, 19.3, 21.2, 28.4, 29.9, 24.3, 18.2, 17.2, 15.2)
g0 <- calcG0(lat=37.2, modeRad='aguiar', dataRad=G0dm)
xyplot(g0)
library(solaR)
## Download data from NREL (see help(calcG0))
NRELurl <- 'http://goo.gl/fFEBN'
dat <- read.table(NRELurl, header=TRUE, sep=',')
names(dat) <- c('date', 'hour', 'G0', 'B', 'D0', 'Ta')
##B is direct normal. We need direct horizontal.
dat$B0 <- dat$G0-dat$D0
@oscarperpinan
oscarperpinan / siar.geojson
Created September 20, 2013 08:05
SIAR stations
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@oscarperpinan
oscarperpinan / siar.R
Created September 20, 2013 08:09
Code to create siar.geojson
library(sp)
library(rgdal)
setwd(tempdir())
download.file('https://raw.github.com/oscarperpinan/solar/gh-pages/data/SIAR.csv', 'siar.csv', method='wget')
siar <- read.csv('siar.csv')
summary(siar)
siarSP <- SpatialPointsDataFrame(siar[,c(6, 7)], siar[,-c(6,7)])
writeOGR(siarSP, 'siar.geojson', 'siarSP', driver='GeoJSON')
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@oscarperpinan
oscarperpinan / stamenPoly.R
Last active December 28, 2015 09:59
Variation of https://gist.github.com/oscarperpinan/5451682 using SpatialPolygons
library(sp)
library(ggmap)
## latticeExtra must be loaded after ggmap because both ggplot2 and
## latticeExtra define a 'layer' function. We need the definition from
## latticeExtra.
library(latticeExtra)
## We only need maptools to get an example
library(maptools)
SIDS <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
IDvar="FIPSNO",