Skip to content

Instantly share code, notes, and snippets.

@rBatt
Last active September 22, 2015 16:20
Show Gist options
  • Save rBatt/b4bfba056e7cf1bec55a to your computer and use it in GitHub Desktop.
Save rBatt/b4bfba056e7cf1bec55a to your computer and use it in GitHub Desktop.
multiply 2d kernel by this
# this is what 'test' looks like:
# > test
# s.reg year num nameID stratum lon lat btemp depth N Nsite spp Z u.a0 a1 a2
# 1: ebs 1982 12 ebs_1982_12 -158.5 57.5 -158.5 57.5 -0.16 34.4 919.4999 83 Acantholithodes hispidus 0 -2.807526 0.60290095 -0.1560553
# 2: ebs 1982 12 ebs_1982_12 -158.5 57.5 -158.5 57.5 -0.16 34.4 919.4999 83 Acantholumpenus mackayi 0 -4.294548 0.66363085 -0.1244255
# 3: ebs 1982 12 ebs_1982_12 -158.5 57.5 -158.5 57.5 -0.16 34.4 919.4999 83 Actinauge verrilli 0 -2.763588 0.67887606 -0.1261046
# 4: ebs 1982 12 ebs_1982_12 -158.5 57.5 -158.5 57.5 -0.16 34.4 919.4999 83 Admete regina 0 -3.658542 0.66688800 -0.1661476
# 5: ebs 1982 12 ebs_1982_12 -158.5 57.5 -158.5 57.5 -0.16 34.4 919.4999 83 Aforia circinata 0 -9.954288 0.67970755 -0.1168343
# ---
# 1188968: ebs 2013 43 ebs_2013_43 -178.5 60.5 -178.5 60.5 1.40 161.0 923.0000 135 Yoldia hyperborea 1 -6.106840 0.19587493 -0.1966885
# 1188969: ebs 2013 43 ebs_2013_43 -178.5 60.5 -178.5 60.5 1.40 161.0 923.0000 135 Yoldia myalis 0 -5.445181 0.24942092 -0.2363827
# 1188970: ebs 2013 43 ebs_2013_43 -178.5 60.5 -178.5 60.5 1.40 161.0 923.0000 135 Yoldia seminuda 0 -6.355285 0.28516302 -0.1895990
# 1188971: ebs 2013 43 ebs_2013_43 -178.5 60.5 -178.5 60.5 1.40 161.0 923.0000 135 Yoldia thraciaeformis 0 -6.527462 0.06389702 -0.2263413
# 1188972: ebs 2013 43 ebs_2013_43 -178.5 60.5 -178.5 60.5 1.40 161.0 923.0000 135 Zaprora silenus 0 -7.043402 -0.12895978 -0.1414058
# a3 a4
# 1: 0.06091388 -0.063946828
# 2: 0.05641806 -0.074596645
# 3: 0.07241147 -0.059670283
# 4: 0.03321085 -0.064787647
# 5: -0.15990573 0.002763577
# ---
# 1188968: 0.07290525 0.015648874
# 1188969: 0.08595705 -0.049931139
# 1188970: 0.07062934 -0.050667106
# 1188971: 0.05769462 -0.044437200
# 1188972: 0.05623803 -0.050197096
pa <- test[,list(stratum, lon, lat, pa=1)]
pa <- pa[!duplicated(stratum)]
setkey(pa, lon, lat)
pa0 <- copy(pa)
pa[,stratum:=NULL]
skeleton <- pa[,expand.grid(lon=seq(min(lon),max(lon),by=0.1), lat=seq(min(lat),max(lat),by=0.1))]
skeleton <- as.data.table(skeleton)
setkey(skeleton, lon, lat)
pa <- pa[skeleton]
pa[is.na(pa), pa:=0]
pa[paste(roundGrid(lon), roundGrid(lat))%in%pa0[,stratum], pa:=1]
reshape2:::acast(pa, lat~lon)
# pa2 <- as.data.frame(pa)
# pa2[is.na(pa2[,"pa"])] <- 0
@rBatt
Copy link
Author

rBatt commented Sep 22, 2015

roundGrid <- function(x, frac=1){
    # if frac is 1, then place in a 1º grid
    # if frac is 0.5, then place in the 0.5º grid
    floor(x/frac)*frac+frac/2
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment