Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201977

Zonal statistic calculating pixel share and determine the most frequent class in R

$
0
0

I already posted a related question here: Zonal statistics to get majority pixel value per polygon in R? , but this time I want to determine the pixel-class with the highest share on the weights in different polygons in a SpatialPolygonsDataFrame. (Similar to QGIS Plugin: zonal statistic > majority) For statistical reasons in my real data set I want to assign the major class at the end to each polygon to the @data slot.

Having some code:

set.seed(6)
# Create interger class raster
r <- raster(ncol=36, nrow=18)
r[] <- round(runif(ncell(r),1,10),digits=0)
r[]<-as.integer(r[])
# Create two polygons
cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
cds2 <- rbind(c(80,0), c(50,30), c(60,0), c(60,-25))
polys <- SpatialPolygonsDataFrame(SpatialPolygons(list(Polygons(list(Polygon(cds1)), 1), 
Polygons(list(Polygon(cds2)),2))),data.frame(ID=c(1,2)))

# Extract raster values to polygons                             
v <- extract(r, polys, weights = TRUE) 

So far everything is fine, but in the further question I just had one vector of values for each list element, so it was possible to count the frequency and determine the name of the class with the highest share.

This time I have two columns in a list : (How is this kind of data-structure inside the list called? Is it a matrix or just the list index?):

[[1]]
...
[[2]]
     value      weight
[1,]    10 0.066071429
[2,]     9 0.085714286
[3,]     8 0.098214286
[4,]     4 0.026785714
[5,]     4 0.178571429
[6,]     2 0.098214286
[7,]     3 0.178571429
[8,]     2 0.107142857
[9,]     3 0.137500000
[10,]     3 0.005357143
[11,]     6 0.017857143

How would I be able to run for example dplyr::group_by() and summarise(). I tried different approaches with dplyr::summarize_at but I am to confused how to handle this data type.


Viewing all articles
Browse latest Browse all 201977

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>