0

My rasterlayer has a range from 0 to 1. I just want the Pixel values from 0.2 to 0.1 I tried this Code:

R<- myraster
R[(R<=0.1) & (R>=0.2)] <- NA

This is my idea for a range of value.

For a single value I don't know.

If I use this code I get online NA or the range from.0 to 1 does not change.

Is my Code wrong or is there another option?

I also used this one only to get the value 0.1

R<- myraster
R[(R<=0.1) & (R>=0.1)] <- NA
Axeman
  • 32,068
  • 8
  • 81
  • 94
Maik
  • 1
  • 1
  • 2
    Do you know of any number that is less than 1 and at the same time greater than 2?That is exactly what your code is doing. Looking for a number that is less than 0.1 and the same number should be greater than 0.2. Can you think of any such number? – Onyambu Jul 14 '22 at 20:46
  • But how do i get a range between two values then? I thought i only get the range from 0.1 to 0.2 if i set all other values NA. – Maik Jul 14 '22 at 21:21
  • 2
    You could simply replace the ‘and’ `&` operator in your first expression with ‘or’ `|`. Does that logic make more sense when you read it out aloud? It might help to quickly read up on Boolean logic to help realise that it doesn’t necessarily map to natural language in the way I think that you think your code is being interpreted. – Michael MacAskill Jul 14 '22 at 21:36
  • Ok for a range it works really good. But what can i do if i just want the pixelvalues ffrom 0.1 – Maik Jul 14 '22 at 22:33
  • You need `R>=0.1 & R<=0.2` – Onyambu Jul 14 '22 at 22:54

1 Answers1

0

You can do it in two steps. For instance,

library(raster)
# Simulate raster
R <- raster(ncol=10, nrow=10)
values(R) <- runif(ncell(R))

#Subset the raster in two steps
R[R >= 0.2] <- NA
R[R <= 0.1] <- NA
R

Here's the output...

> R <- raster(ncol=10, nrow=10)
> values(R) <- runif(ncell(R))
> R
class      : RasterLayer 
dimensions : 10, 10, 100  (nrow, ncol, ncell)
resolution : 36, 18  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : memory
names      : layer 
values     : 0.01307758, 0.9926841  (min, max)

> R[R>=0.2]<-NA
> R[R<=0.1 ]<-NA
> R
class      : RasterLayer 
dimensions : 10, 10, 100  (nrow, ncol, ncell)
resolution : 36, 18  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : memory
names      : layer 
values     : 0.1008731, 0.1912601  (min, max)
CRP
  • 405
  • 2
  • 11