Skip to content

altitudinal overlap

6 messages · Karla Shikev, Stefano Leonardi, Glen A Sargeant

#
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:
[,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
#
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:
======================================================================
  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
#
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

  
  
#
Here you go.
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
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
+   for(j in (i+1):nrow(x)){
+     mat[i,j] <- x$min[j] <= x$max[i]
+   }
+ }
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
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:

            

  
    
#
Oops!  Tally the entire matrix, not just the upper triangular portion.
Adding the code in red (4th line from top) should rectify the error.

for(i in 1:(nrow(x)-1)){
    for(j in (i+1):nrow(x)){
      mat[i,j] <- x$min[j] <= x$max[i]
      *mat[j,i] <- mat[i,j]*
    }
}
On Tue, Jul 14, 2015 at 7:02 PM, Sargeant, Glen <gsargeant at usgs.gov> wrote:

            

  
    
#
Worked like a charm.Thanks!
On Tue, Jul 14, 2015 at 9:31 PM, Sargeant, Glen <gsargeant at usgs.gov> wrote: