[R-sig-Geo] random selection of cells in raster based on distance from xy locations

Michael Sumner mdsumner at gmail.com
Tue Apr 3 08:40:40 CEST 2012


What is the actual error when R "runs out of memory"?

This works with no problems for me (Windows 7, 64-bit R 2.15.0), it
runs in less than a minute, and watching the R process in Task Manager
it never takes up more than about 150Mb.

How much memory does your machine have? If it's 64-bit you ought to
have at least 4Gb, and if you have less that might be the problem.
Otherwise, perhaps you have other memory hungry programs running at
the same time?

Cheers, Mike.

On Tue, Apr 3, 2012 at 3:56 PM, Daisy Englert Duursma
<daisy.duursma at gmail.com> wrote:
> Hi,
>
> I am trying to create an xy table of the center of random cells in a raster
> mask. These cells must occur within a certain distance from xy locations. I
> have successfully found 2 ways of doing this, but both run out of memory (R
> 64-bit version 2.14.0) when using very large datasets. I think I have gone
> about this in a backwards manner and assume there is a simpler way to do
> this.
>
> The following example works, but when I use my actual datasets and create
> lots of overlapping buffers the code runs out of memory. when using the
> function extract.
>
> Thanks,
> Daisy
>
> rm(list = ls())
>
> #load libraries
>
> library(dismo)
> library(raster)
>
> ##example data
> #load map of land
> files<-list.files(path=paste(system.file(package="dismo"),"/ex",sep=""),pattern="grd",full.names=TRUE)
> mask <- raster(files[[9]])
> #make species data
> pts<-randomPoints(mask,2000)
>  #extract the unique cell numbers within a 800km buffer of the points,
> remove NA cells
> #####momory problems#####
> z <- extract(mask, pts, buffer=800000,cellnumbers=T)
> z2<-as.data.frame(do.call(rbind,z))
> z3<-subset(z2, vals == 1, select = cells)
> #find the unique cells
> z4<-unique(z3[,1])
> #get 1000 random cells from the remaining data
> cells<-sample(z4, 1000, replace=FALSE)
> #find lat and long of center point of cells
> xy_locs<-xyFromCell(mask,cells)
>
>
> --
> Daisy Englert Duursma
> Department of Biological Sciences
> Room E8C156
> Macquarie University, North Ryde, NSW 2109
> Australia
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-Geo mailing list