ourMELONS/R/find.R

15 lines
334 B
R
Raw Normal View History

2020-03-18 10:30:37 +01:00
#' @title Find indices and values of nonzero elements
#' @description Emulates behavior of `find`
2020-03-18 15:02:38 +01:00
#' @param x object or logic operation on an object
2021-03-31 10:27:45 +02:00
#' @param sort sort output?
find <- function(x, sort=TRUE) {
2020-07-13 13:23:57 +02:00
if (is.logical(x)) {
2021-03-31 10:27:45 +02:00
out <- which(x)
2020-07-13 13:23:57 +02:00
} else {
2021-03-31 10:27:45 +02:00
out <- which(x > 0)
2020-07-13 13:23:57 +02:00
}
2021-03-31 10:27:45 +02:00
if (sort) {
out <- sort(out)
}
return(out)
2020-03-18 10:30:37 +01:00
}