R/dbase_fn_dom.R

Defines functions dbase_fn

##################
# Function for calculation of the number of degrees to basal species
# Returns a vector of the distance to the base for every species
# Producers have a default distance of 1
# Unconnected species have a distance of 0
# October 2011
# by Dominique Gravel
##################


dbase_fn = function(L) {

  if (nrow(L) !=ncol(L)) stop("This is not a square") ## One way to write an error msg

  stopifnot(nrow(L) == ncol(L)) ## An alternative way of writing an error message

  library(assertthat) ## This package has some good functions, very similar to testthat in theory

  ## This is a nicer way of using stopifnot (which is in base R), this provides a nicer error message
  assertthat::assert_that(nrow(L) == ncol(L))

  NS = nrow(L)

  # Remove cannibalism
  diag(L)=0

  # First, identify the basal species
  nprey = apply(L,2,sum, na.rm = TRUE)
  basal = numeric(nrow(L))
  basal[nprey == 0] = 1
  rk = basal

  # Second, identify who feeds on the basal species
  for(k in 2:10) {
    for(i in 1:NS) {
  	  for(j in 1:NS){
		if(rk[i]==k-1 && rk[j] == 0 && L[i,j]%in%1 && i!=j) rk[j] = k
			}
		  }
		}
	return(rk)
}
linley-sherin/netwerk documentation built on Dec. 21, 2021, 10:50 a.m.