On 22 Sep 2020, at 14:26, ASANTOS via R-sig-Geo <r-sig-geo at r-project.org> wrote:
Dear R-sig-geo Members,
I'd like to use de extract() function using the raster package for
calculate a proportion of pixel with "1"s values inside a buffer given
some coordinates in a raster. I try to create a function for this
without success, in my hypothetical example:
#Package
library(raster)
# Create a raster
ras <- raster(ncol=1000, nrow=1000)
set.seed(0)
values(ras) <- runif(ncell(ras))
values(ras)[values(ras) > 0.5] = 1
values(ras)[values(ras) < 0.5] = NA
# Create some coordinates
pts<-sampleRandom(ras, size=30, xy=TRUE)
pts.df<-as.data.frame(pts)
pts.df$area<-rnorm(30, mean=10)## Here just for create a artificial
covariate without any direct implication in my question
#Function for extract proportion of 1s values
percentual_1s<- function(x,...) {
? leng1<-length(values(x) ==1) # length of 1s pixels values
? lengtotal<-length(x) # total length of pixels inside buffer
? perc<-(leng1/lengtotal)*100
? return(perc)
}
# Extract the desirable proportion in a circular 100000 units buffer
cent_max <- extract(ras, # raster layer
??? cbind(pts.df$x,pts.df$y),??? # SPDF with centroids for buffer
??? buffer = 100000,???????????? # buffer size
??? fun=percentual_1s,?????????? # what to value to extract
??? df=TRUE)
Here doesn't work, despite the code look like Ok. My perfect output is:
#??????? x????? y layer????? area?? percentual_1s
#1 -109.26 -43.65???? 1 10.349010?? 23.15
#2?? 93.42 -87.21???? 1? 9.861920?? 45.18
#3?? 57.06? 86.85???? 1? 8.642071?? 74.32
#4 -109.98 -45.63???? 1 10.376485?? 11.56
#5? -92.34? 37.89???? 1 10.375138?? 56.89
#6?? 19.62? 21.51???? 1? 8.963949?? 88.15
Please any ideas or any another package that help in this operation?
Thanks in advanced,
--
Alexandre dos Santos
Geotechnologies and Spatial Statistics applied to Forest Entomology
Instituto Federal de Mato Grosso (IFMT) - Campus Caceres
Caixa Postal 244 (PO Box)
Avenida dos Ramires, s/n - Vila Real
Caceres - MT - CEP 78201-380 (ZIP code)
Phone: (+55) 65 99686-6970 / (+55) 65 3221-2674
Lattes CV: http://lattes.cnpq.br/1360403201088680
OrcID: orcid.org/0000-0001-8232-6722
ResearchGate: www.researchgate.net/profile/Alexandre_Santos10
Publons: https://publons.com/researcher/3085587/alexandre-dos-santos/
--
[[alternative HTML version deleted]]