Skip to content

Instantly share code, notes, and snippets.

View jlehtoma's full-sized avatar

Joona Lehtomäki jlehtoma

View GitHub Profile
@jlehtoma
jlehtoma / to_equal_area.R
Created September 14, 2015 10:47
Equal area projections of the world in R
library(rworldmap)
library(rgdal)
# Latlon
world_map_wgs84 <- getMap()
plot(world_map_wgs84, main = "WGS84")
proj4string(world_map_wgs84)
# Mollweide
world_map_mollweide <- spTransform(world_map_wgs84, CRS("+proj=moll"))
@jlehtoma
jlehtoma / rmpi_script.R
Created September 14, 2015 09:59
Run simple R/MPI example on CSC Taito
# Tell all slaves to return a message identifying themselves.
mpi.remote.exec(paste("I am",mpi.comm.rank(),"of",mpi.comm.size()))
mpi.remote.exec(paste(mpi.comm.get.parent()))
# Send execution commands to the slaves
x <- 5
x <- mpi.remote.exec(rnorm,x)
length(x)
x
@jlehtoma
jlehtoma / map_europe.R
Created June 29, 2015 13:42
Map Europe
install.packages(c("maptools", "rworldmap", "rworldxtra"))
# Load required packages
library(maptools)
library(rworldmap)
# Define LAEA projection for Europe
crs.laea <- CRS("+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs") # Lambert Azimuthal Equal Area
# Grab the whole world, NOTE that you will also need package rworldextra
@jlehtoma
jlehtoma / scrape_urls.R
Created April 29, 2015 09:51
Scrape bird transect lines urls
library("dplyr")
library("magrittr")
library("rvest")
urls <- c("http://koivu.luomus.fi/seurannat/linjalaskenta/vakiolinjat.php")
site <- html(urls)
linjat_table <- site %>%
html_node("table") %>%
html_nodes("tr")
@jlehtoma
jlehtoma / massage_data.R
Last active August 29, 2015 14:18
Massaging data
library(dplyr)
library(tidyr)
dat <- data.frame(laji=c("accgen", "accnis", "accgen", "accnis" , "accgen",
"accnis", "accgen", "accnis"),
lkm=c(2, 2, 1, 4, 3, 2, 2, 6),
linja_P_E = c("P", "P", "E", "E", "P", "P", "E", "E"))
dat_agg <- dat %>%
# Group by both laji and linja_P_E
@jlehtoma
jlehtoma / join_and_update.R
Last active August 29, 2015 14:18
Join and update transect
library(dplyr)
library(magrittr)
museum_transects <- read.table("data/museon_linjat.csv", as.is = TRUE,
sep = ";", header = TRUE)
standard_transects <- read.table("data/vakioreitit_lista_suojelualueet.csv",
as.is = TRUE, sep = ";", header = TRUE)
# Select only columns Reitti and Suojelualue form standard_transects
# Tell all slaves to return a message identifying themselves.
mpi.remote.exec(paste("I am",mpi.comm.rank(),"of",mpi.comm.size()))
mpi.remote.exec(paste(mpi.comm.get.parent()))
# Send execution commands to the slaves
x <- 5
x <- mpi.remote.exec(rnorm, x)
x
mpi.close.Rslaves()
@jlehtoma
jlehtoma / generate_shx.py
Created January 14, 2015 14:08
Re-create missing .shx-file for ESRI shapefiles
# Based on solution given here: http://geospatialpython.com/2011/11/generating-shapefile-shx-files.html
# Depends on pyshp
# Build a new shx index file
import fnmatch
import os
import shapefile
# List all the shapefiles
def find(pattern, path):

Keybase proof

I hereby claim:

  • I am jlehtoma on github.
  • I am jlehtoma (https://keybase.io/jlehtoma) on keybase.
  • I have a public key whose fingerprint is CCCE BB9B C821 8250 74F4 28BA B49C 7E97 E9C2 A513

To claim this, I am signing this object:

@jlehtoma
jlehtoma / check_list_of_lists_null.R
Created August 26, 2014 19:41
Check if any of the elements in a list within a list is NULL
library(rlist)
x <- list(list("ID"=1, "var1"="B", "var2"="D"),
list("ID"=2, "var1"="F", "var2"="H"),
list("ID"=3, "var1"="I", "var2"=NULL))
# rlist approach - returns FALSE
list.any(x, l ~ is.null(l))
# 2-level lapply - returns TRUE