[R] random sample from list
David Winsemius
dwinsemius at comcast.net
Thu Apr 5 18:57:23 CEST 2012
On Apr 5, 2012, at 12:00 AM, Daisy Englert Duursma wrote:
> random selection of cells in raster based on distance from xy
> locations
>
> Hi,
>
> I am trying to sample a raster for random cells that occur within a
> specific distance of point locations. I have successfully found
> multiple
> ways of doing this but have memory issues with very large datasets. To
> overcome this problem I am working with lists. I am now stuck on how
> to
> randomlly sample the correct elements of a list. Here is an example
> of my
> code and an example dataset.
>
> 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 point data
> pts<-randomPoints(mask,100)
> #extract the unique cell numbers within a 800km buffer of the points,
> remove NA cells
> z <- extract(mask, pts, buffer=800000,cellnumbers=T)
> z_nonna <- lapply(z, na.omit)
>
> ###########PROBLEM AREA##########
> ##If I convert this to a dataframe and find the unique "cells" values
> #z2<-as.data.frame(do.call(rbind,z_nonna))
> #z_unique<-unique(z2[,1])
> ##I can tell there there are 9763 unique "cells" values
> #How do I randomely sample the **LIST** NOT THE DATAFRAME for 5000
> unique
> values from "cells". I am working with huge datasets and the data
> needs to
> stay as a list due to memory issues
>
> #Here is how I have tried to sample but it is not sampling from the
> right
> part of the list
>
> bg<- z_nonna[sample(1:length(z_nonna), 5000, replace=FALSE)]
You should have gotten an error from sample() because z_nonna is only
of length 100.
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list