Skip to content

Instantly share code, notes, and snippets.

View Nowosad's full-sized avatar
🌍

Jakub Nowosad Nowosad

🌍
View GitHub Profile
@joseph-rickert
joseph-rickert / BIG DATA with RevoScale R
Created February 8, 2013 22:39
Scripts for 2/14/13 Webinar Introduction to R for Data Mining
#------------------------------------------------------------
# REVOLUTION ANALYTICS WEBINAR: INTRODUCTION TO R FOR DATA MINING
# February 14, 2013
# Joseph B. Rickert
# Technical Marketing Manager
#
# BIG DATA with RevoScaleR
#
# Copyright: Revolution Analytics
@bryanmayer
bryanmayer / xtableCommand.R
Last active January 1, 2016 21:09
Automatic italics into latex tables using xtable in R
library(xtable)
library(plyr)
fakedata = data.frame(
Bacteria = c("G. vaginalis", "L. crispatus"),
Count = log10(rlnorm(2*100, 13.5))
)
table1 = ddply(fakedata, .(Bacteria), summarize,
Bacteria = paste("\\textit{",unique(Bacteria),"}", sep=""),
# data from http://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/population-distribution-demography/geostat
# Originally seen at http://spatial.ly/2014/08/population-lines/
# So, this blew up on both Reddit and Twitter. Two bugs fixed (southern Spain was a mess,
# and some countries where missing -- measure twice, submit once, damnit), and two silly superflous lines removed after
# @hadleywickham pointed that out. Also, switched from geom_segment to geom_line.
# The result of the code below can be seen at http://imgur.com/ob8c8ph
library(tidyverse)
library(tidycensus)
library(tmap)
library(tmaptools)
library(sf)
library(tigris)
library(magick)
library(tidyverse)
options(tigris_use_cache = TRUE)
ctys <- c("Dallas", "Tarrant", "Collin County", "Denton",
library(gganimate) # thomasp85/gganimate
library(cartogram)
library(geogrid) # Need github version jbaileyh/geogrid
library(rnaturalearth)
library(sf)
library(scico)
us <- ne_states('united states of america', returnclass = 'sf')
us <- us[!us$woe_name %in% c('Alaska', 'Hawaii'), ]
us <- st_transform(us, '+proj=eqdc +lat_0=39 +lon_0=-96 +lat_1=33 +lat_2=45 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs')
@thomasp85
thomasp85 / st_close.R
Last active July 15, 2018 01:34
An algorithm for closing erroneous polygons in sf
st_close <- function(x) {
UseMethod('st_close')
}
st_close.sfg <- function(x) x
st_close.POLYGON <- function(x) {
if (st_is_empty(x)) return(x)
x[] <- lapply(x[], close_mat)
x[vapply(x[], nrow, integer(1)) > 3]
}
st_close.MULTIPOLYGON <- function(x) {
@mdsumner
mdsumner / RasterIO.md
Last active August 21, 2021 11:55
stars example (with raster help) to build the read_stars RasterIO list from an extent
  f <- "ftp.nass.usda.gov/download/res/2019_30m_cdls.img"
  library(raster)
#> Loading required package: sp
crop_io <- function(x, ext, resample = "nearest_neighbour") {
  rx <- raster::raster(x)
  if (inherits(ext, "bbox")) {
    ext <- raster::extent(ext[c("xmin", "xmax", "ymin", "ymax")])
  } else {
    ext <- raster::extent(ext)
## e.g. 
## sf::st_as_text(sf::st_as_sfc(sf::st_bbox(c(xmin = 0, xmax = 1, ymin = 0, ymax = 1))))
wkt_template <- "POLYGON (({xmin} {ymin}, {xmax} {ymin}, {xmax} {ymax}, {xmin} {ymax}, {xmin} {ymin}))"

xmin <- -80; xmax <- -78
ymin <- 34.5; ymax <- 36

## the filter, anything within this box will be included entirely
wkt &lt;- glue::glue(wkt_template)
@Nowosad
Nowosad / st_from_ellipses.R
Created May 29, 2021 14:34
Conversion from a SaTScan text file with ellipses to an sf R spatial object
st_from_ellipses = function(path, crs = "EPSG:25832", res = 30) {
e = read.csv(path)
pnt = sf::st_as_sf(e, coords = c("X", "Y"), crs = crs)
major = pnt$Major
minor = pnt$Minor
angle = pnt$Angle1
rotation = function(a){
r = a * pi / 180 #degrees to radians
matrix(c(cos(r), sin(r), -sin(r), cos(r)), nrow = 2, ncol = 2)
}
isolines_terra_sf <- function(x, levels) {
  if (missing(levels)) {
    levels <- pretty(unlist(minmax(x[[1]])))
    
  }
  ## OMG: note the [[1]] and wide = TRUE which is also weird but different to raster ...
  b <- isoband::isolines(xFromCol(x), yFromRow(x), as.matrix(x[[1]], wide = TRUE), levels = levels)
  
  sf::st_sf(level = levels, geometry  = sf::st_sfc(isoband::iso_to_sfg(b), crs = crs(x)))