Skip to content

Instantly share code, notes, and snippets.

# Simple tibble
d <- tibble(a1="foo",
a2="bar")
# Want to make a new column called "new" and unite all the "a*" columns together
newcol <- "new"
startchar <- "a"
unite(d,col=newcol,starts_with(startchar)) # Yay
@ozjimbob
ozjimbob / him_anim.r
Created January 7, 2020 22:19
Example code to generate animation frames of Himawari-8 hotspots
# General data manipulation
library(tidyverse)
# Spatial data
library(sf)
# Thematic maps
library(tmap)
# ignoring time=t, just modelling on dx for simplicity
frecent3 <- function(rank,dx){
a = 6.1625
cc = 1.6724
b = 0.2882
return(( a/(1e-04 * dx + cc)+b) * rank)
}
library(tidyverse)
library(sf)
library(mapview)
library(furrr)
library(raster)
plan(multiprocess)
ex = extent(c(xmin=-112,xmax=155,ymin=-44,ymax=-10))
@ozjimbob
ozjimbob / access.r
Last active November 21, 2018 10:47
Example of downloading and plotting ACCESS met model data in R
# Demonstration - Downloading and animating ACCESS forecast data
# [email protected]
# General functions, including str_replace, map
library(tidyverse)
# threddscrawler is a useful package for parsing THREDDS data servers
# Install it with:
# devtools::install_github("BigelowLab/threddscrawler")
library(threddscrawler)
library("reticulate")
library(tidyverse)
library(purrr)
# We use the python "holidays" package for the basic holidays
# Installed it using pip first
holidays <- import("holidays")
tas_holidays <- holidays$Australia(prov="TAS")
# List of all dates from 2000 to 2018
data = tibble(foo=c("Bah","Fuzz"))
my_column="foo"
data %>% filter(!!my_column := "Bah")
@ozjimbob
ozjimbob / blitz.r
Last active November 29, 2017 01:09
## Load required packages
# you will need to install these, first do
# eg. install.packages("tidyverse")
# etc. to install them one by one
library(tidyverse)
library(httr)
library(sf)
library(tmap)
@ozjimbob
ozjimbob / sph_hillshade.r
Last active August 16, 2016 05:44
Example code to great a hillshade raster from a DEM in R using spherical environment mapping for the colour scheme.
library(raster)
library(jpeg)
# Spherical environment mapping hillshade function
getv=function(i,a,s){
ct = dim(i)[1:2]/2
sx = values(s)/90 * ct[1]
sy = values(s)/90 * ct[2]
a = values(a) * 0.01745
px = floor(ct[1] + sx * -sin(a))
@ozjimbob
ozjimbob / demiowa.r
Created February 2, 2016 02:39
R Dem Iowa Caucus Graph
library(rjson)
library(httr)
turl="https://iowadems-caucussitecdn-prod2.azureedge.net/api/statecandidateresults"
a=GET(turl)
arr=c(content(a)$StateResults[[1]]$WinPercentage,content(a)$StateResults[[2]]$WinPercentage,content(a)$StateResults[[3]]$WinPercentage)
nam=c(content(a)$StateResults[[1]]$Candidate$DisplayName,content(a)$StateResults[[2]]$Candidate$DisplayName,content(a)$StateResults[[3]]$Candidate$DisplayName)
colr=c(content(a)$StateResults[[1]]$Candidate$Color,content(a)$StateResults[[2]]$Candidate$Color,content(a)$StateResults[[3]]$Candidate$Color)
rar=round(arr,3)*100
barplot(arr,names.arg=nam,col=colr)