[R-sig-eco] altitudinal overlap
Sargeant, Glen
gsargeant at usgs.gov
Wed Jul 15 02:02:32 CEST 2015
Here you go.
> #Data. Each row gives altitudinal limits for a species
>
> x <- matrix(runif(20),10,2)
> x <- t(apply(x,1,sort))
> dimnames(x) <- list(NULL, c("min","max"))
> species <- paste("species",1:10,sep="")
> x <- data.frame(species,x)
> x
species min max
1 species1 0.20903724 0.8232747
2 species2 0.25137925 0.7794901
3 species3 0.09128804 0.2311824
4 species4 0.07792821 0.8668317
5 species5 0.34627541 0.5385044
6 species6 0.14072631 0.3356345
7 species7 0.52154781 0.9216631
8 species8 0.03126684 0.3382409
9 species9 0.43925482 0.4398011
10 species10 0.41146614 0.4611622
> #Order by min and then by max within min
> x <- x[order(x$min,x$max),]
> x
species min max
8 species8 0.03126684 0.3382409
4 species4 0.07792821 0.8668317
3 species3 0.09128804 0.2311824
6 species6 0.14072631 0.3356345
1 species1 0.20903724 0.8232747
2 species2 0.25137925 0.7794901
5 species5 0.34627541 0.5385044
10 species10 0.41146614 0.4611622
9 species9 0.43925482 0.4398011
7 species7 0.52154781 0.9216631
> #Compare each minimum to preceding maxima.
> #This is where the reordering comes in.
> #The only way two species can overlap is if
> #the minimum for the second one is less than
> #or equal to the maximum for the first one.
> mat <- matrix(NA,nrow(x),nrow(x))
> dimnames(mat) <- list(x$species,x$species)
> for(i in 1:(nrow(x)-1)){
+ for(j in (i+1):nrow(x)){
+ mat[i,j] <- x$min[j] <= x$max[i]
+ }
+ }
> mat
species8 species4 species3 species6 species1 species2 species5
species10 species9 species7
species8 NA TRUE TRUE TRUE TRUE TRUE
FALSE FALSE FALSE FALSE
species4 NA NA TRUE TRUE TRUE TRUE
TRUE TRUE TRUE TRUE
species3 NA NA NA TRUE TRUE FALSE
FALSE FALSE FALSE FALSE
species6 NA NA NA NA TRUE TRUE
FALSE FALSE FALSE FALSE
species1 NA NA NA NA NA TRUE
TRUE TRUE TRUE TRUE
species2 NA NA NA NA NA NA
TRUE TRUE TRUE TRUE
species5 NA NA NA NA NA NA
NA TRUE TRUE TRUE
species10 NA NA NA NA NA NA
NA NA TRUE FALSE
species9 NA NA NA NA NA NA
NA NA NA FALSE
species7 NA NA NA NA NA NA
NA NA NA NA
> apply(mat,1,sum,na.rm=TRUE)
species8 species4 species3 species6 species1 species2 species5
species10 species9 species7
5 8 2 2 5 4
3 1 0 0
On Tue, Jul 14, 2015 at 6:37 PM, Karla Shikev <karlashikev at gmail.com> wrote:
> Ok, here's another (a bit more challenging) question.
>
> Imagine that I have a S species and a S x 2 matrix of altitudinal limits
> (min and max elevation). How would you compute the number of co-occurring
> species for each of the S species?
>
> Thanks again for your assistance.
>
> Karla
>
> On Tue, Jul 14, 2015 at 5:14 AM, Stefano Leonardi <
> stefano.leonardi at unipr.it
> > wrote:
>
> >
> > The first think that came to my mind:
> >
> > overlap <- function(v1,v2) {
> > ov <-min(max(v1), max(v2)) - max(min(v1), min(v2))
> > ifelse(ov > 0, ov, 0)
> > }
> >
> > overlap(dat[1,], dat[2,])
> > [1] 0.5
> >
> >
> > Ciao
> > Stefano
> >
> > On 14/07/2015 01:03, Karla Shikev wrote:
> >
> >> Hi there,
> >>
> >> This is a newbie question, and I'm sure there are simple ways to do
> this,
> >> but I've spent my entire afternoon and I couldn't get it to work.
> >>
> >> Imagine that I got the altitudinal range of different species. For
> >> instance:
> >>
> >> dat<-matrix(c(1,3,2.5,4), ncol=2, byrow=TRUE)
> >>> dat
> >>>
> >> [,1] [,2]
> >> [1,] 1.0 3
> >> [2,] 2.5 4
> >>
> >>
> >> The first line indicates that this species is found between 1 and 3,
> >> whereas the second species was found between 2.5 and 4.
> >>
> >> I need a simple way to calculate the overlap of their extents (0.5 in
> this
> >> case). This way should provide 0 if there is no overlap, and it should
> >> also
> >> work in the case where one subject is found only within the extent of
> the
> >> second subject.
> >>
> >> Any help will be greatly appreciated.
> >>
> >> Karla--
> >>
> > ======================================================================
> > Stefano Leonardi
> > Dipartimento di Bioscienze
> > Universita` di Parma
> > Viale Usberti 11a Phone : +39-0521-905659
> > 43124 PARMA (Italy) Fax : +39-0521-905402
> >
> > Il mio photoblog: http://stefanoleonardi.wordpress.com
> > ======================================================================
> >
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>
--
Glen Sargeant, Ph.D.
Research Wildlife Biologist/Statistician
USGS Northern Prairie Wildlife Research Center
E-mail: gsargeant at usgs.gov
Phone: (701) 253-5528
[[alternative HTML version deleted]]
More information about the R-sig-ecology
mailing list