[R-sig-Geo] raster to dataframe with xy=TRUE, na.rm=TRUE

Helen Sofaer helen at rams.colostate.edu
Fri Aug 29 17:22:32 CEST 2014


Thanks Pascal,
That's helpful.

I am curious about what happened in the second example, if anyone else
takes a look.
Cheers,
Helen


On Thu, Aug 28, 2014 at 11:17 PM, Pascal Oettli <kridox at ymail.com> wrote:

> Hello,
>
> Did you try with the "rasterToPoints" function?
>
> Something like:
>
> r1.b.df <- as.data.frame(rasterToPoints(r1.b.mask))
> coordinates(r1.b.df) <- ~x+y
> plot(r1.b.df)
>
> Regards,
> Pascal
>
> On Fri, Aug 29, 2014 at 1:52 PM, Helen Sofaer <helen at rams.colostate.edu>
> wrote:
> > Hi all,
> >
> > I’m trying to convert a RasterBrick to a dataframe while adding the
> > coordinates and while dropping cells that were masked to NA. This
> > combination of options gives me an error when the mask is done with an sp
> > object.
> >
> >
> >
> > Some reproducible code:
> >
> >
> >
> > usa = getData('GADM', country = 'USA', level = 0)
> >
> >
> >
> > r1 = raster()
> >
> > values(r1) = 1:ncell(r1)
> >
> > r1.b = brick(r1, r1, r1, r1)
> >
> > r1.b.mask = mask(r1.b, usa)
> >
> > plot(r1.b.mask)
> >
> > r1.b.df = as.data.frame(r1.b.mask, xy = TRUE, na.rm = TRUE)
> >
> >
> >
> > The error is:
> >
> > Error in data.frame(..., check.names = FALSE) :
> >
> >   arguments imply differing number of rows: 64800, 1109
> >
> >
> >
> > Looks like it wants to combine all the coordinates with just the subset
> of
> > the data.
> >
> >
> >
> > I surprised myself further by looking at what happens if the NAs are in
> > random locations, rather than masked out:
> >
> >
> >
> > # random NAs:
> >
> > r2 = raster()
> >
> > vals = 1:ncell(r2)
> >
> > vals[sample(1:ncell(r2), .5*ncell(r2))] = NA
> >
> > values(r2) = vals
> >
> > plot(r2)
> >
> > r2.b = brick(r2, r2, r2, r2)
> >
> > r2.b.df = as.data.frame(r2.b, xy = TRUE, na.rm = TRUE)
> >
> > str(r2.b.df) # 64800 obs; same as ncell in each layer; further inspection
> > shows all locations are there and some values are repeated
> >
> >
> >
> > Any advice/interpretation is appreciated. In practice I have already
> > cropped but still have a lot of NAs within my extent. Of course, I can
> drop
> > the rows afterwards, but thought I’d ask. Also, I see the same thing if
> > it’s a single layer, rather than a brick.
> >
> >
> >
> > FYI, I’m running 3.1.1 and raster version 2.2-31 and a Mavericks OSX
> (and I
> > also tried it on a Fedora linux machine running 3.1.0 to make sure it
> > wasn’t a Mavericks thing).
> >
> >
> >
> > Thanks for your time!
> >
> > Helen
> >
> > --
> > Helen Sofaer
> > Postdoctoral Fellow
> > Fish Wildlife and Conservation Biology
> > Colorado State University
> >
> >         [[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
>
>
>
> --
> Pascal Oettli
> Project Scientist
> JAMSTEC
> Yokohama, Japan
>



-- 
Helen Sofaer
Postdoctoral Fellow
Fish Wildlife and Conservation Biology
Colorado State University

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list