Permalink
Browse files

slightly faster setting of NA in diversity (+ whitspace)

issue #239 in github
  • Loading branch information...
1 parent 28aeba1 commit 522a20a6495e7e99bfa9c00830ead3f1233cf978 @jarioksa jarioksa committed Jun 28, 2017
Showing with 6 additions and 6 deletions.
  1. +6 −6 R/diversity.R
View
@@ -1,5 +1,5 @@
`diversity` <-
- function (x, index = "shannon", MARGIN = 1, base = exp(1))
+ function (x, index = "shannon", MARGIN = 1, base = exp(1))
{
x <- drop(as.matrix(x))
if (any(x < 0, na.rm = TRUE))
@@ -16,16 +16,16 @@
x <- -x * log(x, base)
else
x <- x*x
- if (length(dim(x)) > 1)
+ if (length(dim(x)) > 1)
H <- apply(x, MARGIN, sum, na.rm = TRUE)
else
H <- sum(x, na.rm = TRUE)
- if (index == "simpson")
+ if (index == "simpson")
H <- 1 - H
- else if (index == "invsimpson")
+ else if (index == "invsimpson")
H <- 1/H
## check NA in data
- if (any(is.na(total)))
- is.na(H) <- is.na(total)
+ if (any(NAS <- is.na(total)))
+ H[NAS] <- NA
H
}

0 comments on commit 522a20a

Please sign in to comment.