haotu : an open lab notebook

2016/04/25

assign nearest point to a polygon

Filed under: R, R spatial, R Stats, Uncategorized — S @ 11:31

 

wer<-SpatialPointsDataFrame(data.frame(x.subset[,"longitude"],x.subset[,"latitude"]),data=data.frame(x.subset))
n <- length(wer)
nearest <- character(n)
for (i in seq_along(nearest)) {
 nearest[i] <- names(polys)[which.min(gDistance(wer[i,], polys, byid=TRUE))]
}

This may give you warnings if your code is not projected into a planar coordinate system

http://stackoverflow.com/questions/26308426/how-do-i-find-the-polygon-nearest-to-a-point-in-r

Advertisements

2016/04/22

add unique id to duplicated grouped rows

Filed under: Manipulate Data in R, R, R, Uncategorized — S @ 12:30
library(plyr)
id(x)

2016/04/19

get latitude longitude raster cells

Filed under: R, R graphics, R spatial, Uncategorized — S @ 07:46
rasterToPoints(stacked) #this also gives the cell values of the raster layer or layers

http://gis.stackexchange.com/questions/142156/r-how-to-get-latitudes-and-longitudes-from-a-rasterlayer

 

 

number of layers in raster stack

Filed under: R, R graphics, R spatial, R Stats, Uncategorized — S @ 07:08
dimension(stacked)[3]

2016/04/18

stack rasters with different extents etc..

Filed under: arcmap, R, R, R spatial, R Stats, Uncategorized — S @ 13:42
two<-resample(two,one)
stack(one,two)

raster cell area size 1 degree cell size km2

Filed under: R, R spatial, R Stats, Uncategorized — Tags: , — S @ 08:14

For a 1×1 degree cell size raster

r <- raster(ncol=360, nrow=180) #or just the default r<-raster()
area(r)
area(r)$values

 

http://gis.stackexchange.com/questions/177622/r-calculate-raster-cell-size-in-map-units

 

http://gis.stackexchange.com/questions/29734/how-to-calculate-area-of-1-x-1-degree-cells-in-a-raster

2016/04/12

R project to R package Rstudio

Filed under: R, R Stats, Uncategorized — S @ 08:18

This is OK

http://www.r-bloggers.com/building-a-package-in-rstudio-is-actually-very-easy/

  1. Start with a Github repository. This will have a set of R sourcecode files, which collectively do something interesting.
  2. Within RStudio, create a new project, using your local Github repository as the project’s source directory.
  3. Within the “Build Tools” section of the “Project Options” make sure that you’ve selected “Package” from the “Project Build Tools” dropdown box.
  4. In that same dialog box, enter the “–no-examples” flag of the “Check Package” build options. (At this stage, you probably don’t have any examples composed. The default assumes that you do. I don’t know why this is.)
  5. If RStudio, didn’t create one, create a subdirectory in your project folder called “R”. Move all your code here. Github will reflect the deletion and new file creation.
  6. Create a DESCRIPTION file. There are numerous sources which explain how to do this.
  7. Make sure your code doesn’t have any errors in it. Easiest way to do that is to source all of them.
  8. Build the package.

A nice Video here:

Also this devtools checklist

https://www.rstudio.com/wp-content/uploads/2015/06/devtools-cheatsheet.pdf

 

2016/03/01

select multiple columns of a data.table by column name

Filed under: Manipulate Data in R, R, R Stats — Tags: , — S @ 12:39
myDT[,.(mycolname1,mycolname2,mycolname3)]

2016/02/22

true copy of a data.table

Filed under: Manipulate Data in R, R — S @ 10:13
mycopy<-copy(DT)

2016/02/19

Join polygons like countries from mapdata

Filed under: arcmap, R, R, R spatial — S @ 13:28
library(maps)
library(mapdata)
library(maptools)
library(rgeos)

DR<-map(database = 'worldHires',regions="Dominican Republic",fill=TRUE)
 IDs<-DR$names
 DR<-map2SpatialPolygons(DR,IDs, proj4string=CRS("+proj=longlat +datum=WGS84"))
Haiti<-map(database = 'worldHires',regions="Haiti",fill=TRUE)
 IDs<-Haiti$names
 Haiti<-map2SpatialPolygons(Haiti,IDs, proj4string=CRS("+proj=longlat +datum=WGS84"))
Hispaniola<-gUnion(DR, Haiti)
plot(Hispaniola)

Note fill=TRUE

« Newer PostsOlder Posts »

Create a free website or blog at WordPress.com.