It looks like the problem is calling the argument 'extent' rather than
'ext' in sampleRandom.

library(raster)

r <- raster(nrow = 50, ncol = 50)
r[] <- 1

e <- extent(0, 50, 0, 50)

samp1 <- sampleRandom(r, size = 50, cells = TRUE, extent = e)
samp2 <- sampleRandom(r, size = 50, cells = TRUE, ext = e)

r[samp1[,1]] <- 100 #change values of samp1 cells to highlight in plot
r[samp2[,1]] <- 200 #change values of samp2 cells to highlight in plot

plot(r)
plot(e, add = TRUE, col = 'blue')
points(xyFromCell(r, samp1[,1]), col = 'red')
points(xyFromCell(r, samp2[,1]), col = 'black')

##yellow with red circles on plot created using 'extent'
##green with black circles on plot created using 'ext'



On Mon, Nov 25, 2013 at 10:04 AM, Robert J. Hijmans <r.hijmans@gmail.com>wrote:

> Ned,
>
> Hard to see what does not work here. Perhaps it is just the [i] in
> xyFromCell(predImage, sampleCells[i])?
> Can you please provide a self-contained example, perhaps based on the
> below?
>
> library(raster)
> r <- raster(system.file("external/rlogo.grd", package="raster"))
> e <- extent(r) + 100
> s <- sampleRandom(r, size=100, cells=TRUE, extent=e)[,1]
> xy <- xyFromCell(r, s)
>
> plot(e)
> plot(r, add=T, legend=F)
> points(xy)
>
>
>
> On Mon, Nov 25, 2013 at 9:42 AM, Ned Horning <horning@amnh.org> wrote:
> > Hi -
> >
> > I have a script that calculates a common extent between two images than
> then
> > uses the resulting extent to restrict the random selection of cells from
> a
> > raster using the “sampleRandom” function. I use the following function:
> >
> > sampleCells <- sampleRandom(predImage, size=numSamps, cells=TRUE,
> > extent=commonExt)[,1]
> >
> > Next I use "xyFromCell(predImage, sampleCells[i])" to calculate the
> > coordinates of the center of a cell and all of the cell centers fall
> outside
> > of the extent that I am using. Using "sp=TRUE" I get a similar result -
> all
> > of the points are outside of the extent:
> >
> > sampleCells <- sampleRandom(predImage, size=numSamps, sp=TRUE,
> > ext=commonExt)
> >
> > To get around this problem I tried to physically clip the image before
> > running “sampleRandom” and the results are similar. The following line
> also
> > produces cells that are outside of the extent “commonExt” when I use
> > “xyFromCell”:
> >
> > sampleCells <- sampleRandom(crop(predImage,commonExt), size=numSamps,
> > ext=commonExt, na.rm=TRUE)
> >
> > However, if I run the following line using"clip" and "sp=TRUE" the sample
> > points fall within the extent:
> >
> > sampleCells <- sampleRandom(crop(predImage,commonExt), size=numSamps,
> > sp=TRUE, ext=commonExt, na.rm=TRUE)
> >
> > So, there is a work-around but I'd prefer not to have the overhead of
> > clipping the image.
> >
> > This seems somewhat similar to a problem I reported a few months ago and
> was
> > fixed. Can anyone replicate this problem? Am I doing something wrong?
> >
> > I am using R version: 3.0.2 and Raster package version: 2.1-49 on Ubuntu
> > 12.04 64 bit
> >
> > Ned
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo@r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
*Andrew P. Vitale*
Masters Student
Department of Geography
University of Nevada, Reno
vitale232@gmail.com

	[[alternative HTML version deleted]]

