[R-sig-Geo] Daisy- select cells in a raster and change the values to 1

ADAM PETER CARDILINI apcar at deakin.edu.au
Tue Mar 20 03:32:02 CET 2012


G'day Daisy,

My first time replying to a question, so I hope I don't give you the wrong info.

You could try:

library(raster)
r <- raster(ncol=3, nrow=3)
r[] <- 1:ncell(r)

#the first set of numbers is a list of your values, the second set is what you want those values to become.
isBecomes<-cbind(c(1,2,3,4,5,6,7,8,9),c(NA,1,NA,1,NA,1,NA,NA,NA))
r2 <- reclass(r, rcl=isBecomes)

I hope this is what you are looking for. Good luck with it.

Cheers,
Adam


Greetings,

I have a vector of values and I want to select cells in a raster that have
these values, change these cells so they have a value of 1 and the rest of
the raster will have a NA value.

library(raster)

#my example raster
r <- raster(ncol=3, nrow=3)
r[] <- 1:ncell(r)
#a vector of cells I want to select
vals<-c(2,4,6)

#I want to select cells with the values 2,4,6 a silly way to do it:
r1<-r==2
r2<-r==4
r3<-r==6
#make the final desired raster
r4<-r1+r2+r3
plot(r4)

#I have played around with a lot of differnt things but I think I want to
use the funcion sub
######this does not work######
df = data.frame(id=c(2,4,6), v=c(1,1,1))
x = subs(r, df)



Is there a better way to do this? There must be a better way. What have I
missed? I need to be able to select raster cells from a vector.

Thank you for your time and 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]]



------------------------------

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


End of R-sig-Geo Digest, Vol 103, Issue 19



More information about the R-sig-Geo mailing list