[R] Extracting xy from raster based on raster value

Michelle Greve michelle_greve at yahoo.com
Mon Feb 23 17:11:39 CET 2009


Dear Kingsford
Thank you very much for your suggestion. It turns out I had downloaded an old version of raster. I also got a reply from the R-forge website with other suggestions.
In case anybody using the R-help list is interested, here is the link:
http://r-forge.r-project.org/forum/message.php?msg_id=1269

Regards,
Michelle



----- Original Message ----
From: Kingsford Jones <kingsfordjones at gmail.com>
To: Michelle Greve <michelle_greve at yahoo.com>
Cc: R-help at r-project.org
Sent: Sunday, February 22, 2009 7:32:58 PM
Subject: Re: [R] Extracting xy from raster based on raster value

Hi Michelle,

The r-sig-geo list would be the ideal place for this question since
the authors of 'raster' would be likely to see it.

I haven't used the raster package before, but this seems to work:


#install.packages("raster", repos="http://R-Forge.R-project.org")
r <- raster()
r <- setValues(r, sample(1:27, ncell(r), replace=1))
plot(r)
r[] <- r[]==1
plot(r)

If you'd rather have the non-1 cells be NA, just replace 'r[]==1' with
'ifelse(r[]==1, 1, NA)'

hth,

Kingsford Jones


On Sat, Feb 21, 2009 at 8:46 AM, Michelle Greve
<michelle_greve at yahoo.com> wrote:
> I have a raster (which I called glc), which I read
> into R as a raster with "raster.create.from.file" from the raster package (R-forge). Values in glc range between 1 and 27 (whole numbers only). I'd like to extract all cells with a value of 1 to create
> a new raster with only the cells that have a value of 1, or to extract
> the xy values of all raster cells with a value of 1. Could you please
> help me along? I have tried using several of the functions in the
> raster help package, e.g. "raster.get.xy.from.cell", but do not know
> how to specify glc=1. I have tried to use e.g.:
> glc1<-raster.get.xy.from.cell(glc,glc==1)
> glc1<-raster.get.xy.from.cell(glc, glc[which(glc==1)])
>
> The reason I specifically used the raster package to read in my files is because I have been unable to read my ascii files (which are very big) into R using read.asciigrid because the ascii files are so big (and none of the various suggestions I found in various R-help archives that I found for increasing memory size were sufficient to allow me to read in my ascii files). Therefore if alternatively somebody knows of another way to read raster or ascii files into R, which would make it easier to conduct the extraction I mentioned in my first paragraph, I would appreciate any suggestions.
>
> Regards,
> Michelle
>
> ______________________________________________
> R-help at 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.
>



  



More information about the R-help mailing list