Skip to content

Instantly share code, notes, and snippets.

View jlehtoma's full-sized avatar

Joona Lehtomäki jlehtoma

View GitHub Profile
@jlehtoma
jlehtoma / read_subset_csv.R
Created May 13, 2013 13:58
Reading and subsetting data from csv files
library(plyr)
# Generate some data ------------------------------------------------------
set.seed(66)
# Define the folder from which to read
csv.folder <- "PATH_TO_CSV_FOLDER"
# Number of hila elements
@jlehtoma
jlehtoma / WGS84toECS.R
Created March 26, 2013 11:11
Transfrom WGS84 (lat/lon) coordinates into Estonian Coordinate System
# Install dependencies if not present
deps <- c("ggmap", "sp", "rgdal", "XLConnect")
install.packages(deps)
for (dep in deps) {
library(dep, character.only=TRUE)
}
# Read the data
xlsx.file <- "/home/jlehtoma/temp/Points to be converted.xlsx"
@jlehtoma
jlehtoma / unpivot.R
Created March 11, 2013 18:21
Unpivot RLI habitat mappings
library(reshape)
# Generate some toy data
df.kemera <- data.frame("Habitat_RL"=c("Mrf", "Mrg", "Ik", "Ij"),
"Lehto"=c(0, 1, 0, 0),
"Paahde"=c(1, 0, 0, 1),
"Lähde"=c(0, 0, 1, 0),
"Letto"=c(1, 0, 0, 0))
df.natura <- data.frame("Habitat_RL"=c("Mrf", "Mrg", "Ik", "Ij"),
"Heinävarpu"=c(0, 0, 1, 1),
# KML-filun määrityksen mukaan enkoodaus on UTF-8
> head -n 1 Kuntarajat\ 2011\ \&\ 2012\ .kml
<?xml version='1.0' encoding='UTF-8'?>
# Fileen oikea enkoodaus on kuitenkin iso-8859-1
> file -bi Kuntarajat\ 2011\ \&\ 2012\ .kml
application/xml; charset=iso-8859-1
# Muutetaan enkoodaus iso-8859-1 -> UTF-8
> iconv -f iso-8859-1 -t utf8 Kuntarajat\ 2011\ \&\ 2012\ .kml > Kuntarajat_2011_2012_utf8.kml
@jlehtoma
jlehtoma / wind_SE.R
Last active December 14, 2015 08:19
How big a fraction of wind directions fall into South-East direction
# Read in the data
dat <- read.csv("...", sep=";", header=T)
# Calculate the percentage values for each 10° category
# Divide each row n value with the sum of all n's
# Round to precision 3, this will cause a slight
# rounding error
dat$percent <- round(dat$n / sum(dat$n) * 100, 3)
# Get just the South-East [90°, 180°]
@jlehtoma
jlehtoma / euref2wgs84.R
Last active December 13, 2015 23:49
ETRS-TM35FIN -> WGS84
library("sp")
library("rgdal")
# df is a data frame holding the coords (in ETRS-TM35FIN) in east (easting) and north (northing) fields
sp.data <- SpatialPointsDataFrame(df[, c(north, east)], df, proj4string = CRS("+init=epsg:3067"))
# Project the spatial data to lat/lon
sp.data.wgs84 <- spTransform(sp.data, CRS("+proj=longlat +datum=WGS84"))
.get.format <- Vectorize(function(x) {
if (nchar(x) == 4) {
return("%H%M")
} else if(nchar(x) == 6) {
return("%H%M%S")
} else {
warning(paste("Time String should be either HHMM or HHMMSS:", x))
return(NA)
}
@jlehtoma
jlehtoma / testrunner.py
Created October 2, 2012 10:54
Test zig3.exe crahses
# -*- coding: utf-8 -*-
'''
Windows error reporting dialog must be disabled for this to work:
1. Press WIN+R
2. Type in "gpedit.msc"
3. Computer configuration -> Administrative Templates
4. Windows Components -> Windows Error Reporting
5. Set "Prevent display of the user interface for critical errors" to Enabled
'''
@jlehtoma
jlehtoma / pxr_crash.R
Created September 23, 2012 07:47
pxR crashes R with certain kinds of PX-Axis files
library(pxR)
# Finish municipal election data from Statistics Finland
good.px.url <- "http://pxweb2.stat.fi/database/StatFin/vaa/kvaa/2008_05/610_kvaa_2008_2009-10-30_tau_137_fi.px"
bad.px.url <- "http://pxweb2.stat.fi/Database/StatFin/vaa/kvaa/2008_04/410_kvaa_2008_2009-11-02_tau_123_fi.px"
# Works
good.px.object <- read.px(good.px.url)
# Crashes R
@jlehtoma
jlehtoma / trouble.Rmd
Created September 3, 2012 14:39
Trouble with RStudio, Knitr and table()
```{r data_load, warning=FALSE, echo=FALSE, cache=FALSE}
actual.data <- load.finrisk(fields)
```
```{r rows_per_year, echo=TRUE, cache=FALSE}
# How many rows for each year
manual.data <- c(rep(1992, 6031), rep(1997, 8350), rep(2002, 8491), rep(2007, 6115))
manual.df <- data.frame(VAR1=manual.data)
table(manual.df$VAR1)