Hi Lyndon,
You are right it works for my data because my data is integer as shown: 
Data attributes:
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max.     NA's 
    1.00     4.00     5.00     5.31     6.00     9.00 20735.00 

Question 1 is answered i managed to remove the class 9 so my landcover map is 
now having 8 classes. but i can not collapse these 8 classes into one because i 
will loose the classes on the legend.
What i want to do now is to create a mask  by using the new landcover which is 
now RasterLayer ( because of this script m3 <- raster(landcover, layer = 
"band1", m4 <- m3 * ((m3 < 9) / (m3 < 9)) and country. I am still a beginner in 
using R .

Question 
how can i create a mask from 2 different datasets which are:
SpatialPolygonsDataFrame(country) defining the extend of study + the 
wanted sections are  under the new landcover map which is a 
SpatialGridDataFrame 
= mask

Is this possible in R?

Aim of creating this mask is to clip the other 5 datasets like DEM which are 
spatialgriddataframes.

Thanks

Mary



________________________________
From: Lyndon Estes <les...@princeton.edu>

Cc: r-sig-geo-ow...@r-project.org; r-sig-geo@r-project.org
Sent: Sun, February 20, 2011 5:44:54 PM
Subject: Re: [R-sig-Geo] spatialgridataframes

Hi Mary,

This might help answer your questions. I used the meuse dataset and
converted to raster formats, but I think the general approach should
work for what you want to do.

library(raster)
library(gstat)
data(meuse.grid)
coordinates(meuse.grid) = ~x+y
gridded(meuse.grid) = TRUE
class(meuse.grid)
m2 <- as(meuse.grid, "SpatialGridDataFrame")
m3 <- raster(m2, layer = "soil")  # Convert soil classes to raster

>
> Question1.
> How can i remove the ninth class in R because it does not have to be included 
>in
> geostatical analysis.

m4 <- m3 * ((m3 < 3) / (m3 < 3))  # Removes class 3 from soil,
converts it to NA values (this could also
# serve as a mask)

# If you want to keep that part of the grid in the analysis, then you
might want to collapse the one class
# into another
m5 <- (m3 == 3 | m3 == 2) * 2 + (m3 == 1)  # Class 2 now includes 2 and 3

> Question 2
> how can i use the map created in Q1 to clip the other 5  aboventioned maps (eg
> DEM etc) or how can i create a mask from the map in Q1.
>


# Create a mask for just the area of soil class 1
sc1.mask <- (m3 == 1) / (m3 == 1)

# You then multiply your other rasters by your mask to reduce rasters
to the areas you want to analyze.

Cheers, Lyndon



      
        [[alternative HTML version deleted]]

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

Reply via email to