[R-sig-Geo] problem with findInterval when mapping in a loop
Edzer Pebesma
edzer.pebesma at uni-muenster.de
Wed Apr 2 18:59:50 CEST 2014
see ?findIntervals: vec refers to the second argument, i.e. bins in your
call. That one has to be sorted. Why do you think that mean-sd is larger
than min? Most things in this world are not normally distributed.
On 04/02/2014 06:28 PM, Alessandra Carioli wrote:
> Dear Bloggers,
> I’ve been working with a for loop to create maps of dependent variables to speed up a bit the work but I keep getting this error message
>
> Error in findInterval(varofint2, bins, rightmost.closed = T) : 'vec' must be sorted non-decreasingly
>
> I am not sure what to do (I have tried sorting the variables, just to see what happens but I still get the message).
> Any help on the matter is very much appreciated!
>
> Ale
>
> R code:
>
> library(spdep)
> library(RColorBrewer)
>
> var.data<- data.frame(var1,var2,var3,var4,var5,var6,var7,var8,var9)
> var.names<-c(“name variable 1”,…,”name variable 9"
>
> m <- length(var.data)
> var.color <- matrix(nrow=n,ncol=m, data=rep(0,n*m))
> for (i in 1:m) {
> varofint <- var.data[,i]
> mean <- mean(varofint)
> sd <- sd(varofint)
> min <- min(varofint)
> max <- max(varofint)
> bins <- c(min,mean-sd, mean, mean+sd, max)
> lb <- length(bins)
> med <- median(1:lb)
> colpal <- brewer.pal(lb, "PRGn")[-med]
> var.color[,i] <- colpal[findInterval(varofint, bins, rightmost.closed=T)] #this is what does not work
> plot(shape.shp, col=var.color[,i], bg="grey5",lty=0) #shape.shp name of shape file
> title(var.names[[i]], cex=1)
> legend("bottomright",fill=colpal,legend=paste(round(bins[-length(bins)],2),
> "-", round(bins[-1],2)),cex=0.7, bg="white")
> }
> _______________________________________________
> 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
Heisenbergstraße 2, 48149 Münster, Germany. Phone: +49 251
83 33081 http://ifgi.uni-muenster.de GPG key ID 0xAC227795
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20140402/6760820e/attachment.bin>
More information about the R-sig-Geo
mailing list