R/betahab.R

Defines functions betahab

#' Function for computing beta diversity within islands or plots
#'
#' @ indat = input data frame
#' @ from = character vector with column number where to start computations from
#' @ group = grouping factor i.e. island or plot or whatever else
#' @ habitats = character vector containing habitat information
#'


betahab<-function(indat,from,group,sp.habitats){
  # store results
  results<-NULL
  # Extract data for one island
  indat<-as.data.frame(indat)
  # loop through habitats
  for (z in 1:length(sp.habitats)){
    # filter by selected habitat
    indat1<-data.frame(indat[,1:from-1],indat[,colnames(indat) %in% sp.habitats[[z]]])
    # island list
    isl.l<-unique(indat1[,group[1]])
    for (i in 1:length(isl.l)){
      # filter by first criterion (Island)
      tmp<-indat1[indat1[,group[1]] %in% isl.l[i],]
      tmp1<-as.data.frame(tmp)
      tmp2<-tmp1[,from:dim(tmp1)[2]]
      tmp3<-tmp2[,names(colSums(tmp2)[colSums(tmp2) > 0])]
      tmp4<-beta.multi(tmp3,abund=FALSE)
      tmp5<-data.frame(group=isl.l[i],comp=row.names(tmp4)[2:3],
      value=tmp4[2:3,1],hab=names(sp.habitats)[z])
      results<-rbind(tmp5,results)

    }
  }
  return(results)
}
marcog77/Rmacdiv documentation built on March 20, 2018, 5:25 a.m.