An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110220/dd265958/attachment.ksh>
spatialgridataframes
9 messages · Edzer Pebesma, Lyndon Estes, Matthias Hinz +2 more
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
Lyndon, the original question was about removing a particular level in a
factor variable. The answer you gave seems to work for numeric variables
only (funny enough, soil in meuse.grid is a numeric variable!), and
package raster doesn't seem to deal well with factors (although coercion
to raster does not complain). Building on your example:
meuse.grid$soilf = factor(meuse.grid$soil, labels=c("cl1","cl2","cl3"))
m2 <- as(meuse.grid, "SpatialGridDataFrame")
m3 <- raster(m2, layer = "soilf") # Convert soilf factor to raster
plot(m3)
It seems that here, the factor levels are lost, as well as the knowledge
that this was a factor, at least it is lost when converted back to a
SpatialGridDataFrame.
Function factor can be used to change factors, or factor levels, or
remove some, as in:
meuse.grid$soilf2 = factor(meuse.grid$soilf,
levels=levels(meuse.grid$soilf)[1:2])
Working with factors converted to numeric values in linear regression
models typically leads to plain wrong results.
On 02/20/2011 05:44 PM, Lyndon Estes wrote:
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
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Edzer Pebesma Institute for Geoinformatics (ifgi), University of M?nster Weseler Stra?e 253, 48151 M?nster, Germany. Phone: +49 251 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de http://www.52north.org/geostatistics e.pebesma at wwu.de
Hi Edzer, Thanks for the clarification regarding factors. Cheers, Lyndon On Sun, Feb 20, 2011 at 3:50 PM, Edzer Pebesma
<edzer.pebesma at uni-muenster.de> wrote:
Lyndon, the original question was about removing a particular level in a
factor variable. The answer you gave seems to work for numeric variables
only (funny enough, soil in meuse.grid is a numeric variable!), and
package raster doesn't seem to deal well with factors (although coercion
to raster does not complain). Building on your example:
meuse.grid$soilf = factor(meuse.grid$soil, labels=c("cl1","cl2","cl3"))
m2 <- as(meuse.grid, "SpatialGridDataFrame")
m3 <- raster(m2, layer = "soilf") ?# Convert soilf factor to raster
plot(m3)
It seems that here, the factor levels are lost, as well as the knowledge
that this was a factor, at least it is lost when converted back to a
SpatialGridDataFrame.
Function factor can be used to change factors, or factor levels, or
remove some, as in:
meuse.grid$soilf2 = factor(meuse.grid$soilf,
levels=levels(meuse.grid$soilf)[1:2])
Working with factors converted to numeric values in linear regression
models typically leads to plain wrong results.
On 02/20/2011 05:44 PM, Lyndon Estes wrote:
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
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
-- Edzer Pebesma Institute for Geoinformatics (ifgi), University of M?nster Weseler Stra?e 253, 48151 M?nster, Germany. Phone: +49 251 8333081, Fax: +49 251 8339763 ?http://ifgi.uni-muenster.de http://www.52north.org/geostatistics ? ? ?e.pebesma at wwu.de
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Lyndon Estes Research Associate Woodrow Wilson School Princeton University +1-609-258-2392 (o) +1-609-258-6082 (f) +1-202-431-0496 (m) lestes at princeton.edu
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110221/634c47f6/attachment.pl>
Hi Mary, I am not sure that I totally understand how you want to mask, so my answer will probably be confused here. If I understand you correctly, the landcover dataset extends outside of the country you are studying, so you just want to limit the analysis to certain landcovers falling within that country. It sounds to me as if you have already selected the landcovers you want, so I would again coerce both the country and "mask" to rasters (maybe Edzer or somebody else can correct me if this is problematic). Since it sounds like the landcover is bigger than the country, I would use mask's extent to define the raster for "country" mask.r <- raster(mask) Presumably you have a polygon for the country, so to me it's easiest to do this: cnt.r <- rasterize(SpatialPolygons(country), mask.r) Then to use both to make a mask, you could do something like this: my.mask <- mask.r & cnt.r That should give you a mask for the landcovers you want within your country. Hope this works and is relevant. Cheers, Lyndon
On Mon, Feb 21, 2011 at 12:07 PM, Mary Rise <risemary48 at yahoo.com> wrote:
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 <lestes at princeton.edu> To: Mary Rise <risemary48 at yahoo.com> Cc: r-sig-geo-owner at r-project.org; r-sig-geo at 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
Lyndon Estes Research Associate Woodrow Wilson School Princeton University +1-609-258-2392 (o) +1-609-258-6082 (f) +1-202-431-0496 (m) lestes at princeton.edu
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110221/7e2667d0/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110222/828cc3b5/attachment.pl>
Hello friends, Some advice might be very helpful A have a shapefile from a certain region. I used the "spdep" package to obtain the objects necessary to my spatial analysis (nb, listw, etc). Now I was wondering if there is a way to split the map into several other maps (in two, three or even four parts) and repeat the study for the subregions in a separated way. Moreover, I would like to know if It is possible to break the shapefile in parts (like in little shapefiles) and apply the "spdep" functions to it? Thanks for the help! Roberto "Esta mensagem e uma correspond?ncia reservada e sua divulga??o, distribui??o, reprodu??o ou qualquer forma de utiliza??o depende de autoriza??o, sujeitando-se o respons?vel a medidas judiciais. O remetente utiliza o correio eletr?nico no exerc?cio do seu trabalho ou em raz?o dele, eximindo esta institui??o de qualquer responsabilidade por utiliza??o indevida. Se voc? a recebeu por engano, favor elimin?-la." "This message is a reserved correspondence and its disclosure, distribution, reproduction or any other form of use shall depend upon proper authorization, and the recipient responsible for such disclosure, distribution, reproduction or use shall be subject to legal actions. The sender uses the electronic mail in the exercise of his/her work or by virtue thereof, and the institution accepts no liability for its undue use. If you have received this e-mail by mistake, please delete it."