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

Helen Sofaer helen at rams.colostate.edu
Fri Aug 29 06:52:11 CEST 2014


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



More information about the R-sig-Geo mailing list