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]]
Hello,> > #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 are sampling from the length of z_nonna, with no guarantee that they are indices to unique list elements. Try this. # First, create some fake data. n <- 1000 z <- list() set.seed(1234) for(i in 1:n) z[[i]] <- sample(letters, 2) # Now sample some unique elements from it. iz <- which(!duplicated(z)) iz <- sample(iz, 100) # sample from the non-duplicate indices. z[iz] Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/random-sample-from-list-tp4533936p4535397.html Sent from the R help mailing list archive at Nabble.com.
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