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)]


Thanks for the help,

Daisy



-- 
Daisy Englert Duursma
Department of Biological Sciences
Room E8C156
Macquarie University, North Ryde, NSW 2109
Australia

Tel +61 2 9850 9256

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to