Skip to content

Commit

Permalink
handle NA values (when na.rm=FALSE) in aggregation of thickness for…
Browse files Browse the repository at this point in the history
… dominant condition
  • Loading branch information
brownag committed Oct 11, 2024
1 parent 268497d commit 1a84883
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion R/collapseHz.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,22 @@ collapseHz <- function(x,
# take thickest value
# v[which.max(bottom - top)[1]]

# convert factors etc to character
# results may not conform with existing factor levels
v <- as.character(v)

# replace NA values for use in aggregate()
if (!na.rm) {
v[is.na(v)] <- "<collapseHZ-category-missing>"
}

# take dominant condition (based on sum of thickness)
cond <- aggregate(bottom - top, by = list(as.character(v)), sum, na.rm = na.rm)
cond <- aggregate(bottom - top, by = list(v), sum, na.rm = na.rm)
v <- cond[[1]][which.max(cond[[2]])[1]]

if (!na.rm) {
v[v == "<collapseHZ-category-missing>"] <- NA
}
}
}
out <- data.frame(v)
Expand Down

0 comments on commit 1a84883

Please sign in to comment.