Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uniqueN escape forder to base R for small atomic vectors, #1120 #3743

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion R/duplicated.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,25 @@ uniqueN = function(x, by = if (is.list(x)) seq_along(x) else NULL, na.rm=FALSE)
by = key(x)
stop(error_oldUniqueByKey)
}
#verbose = getOption("datatable.verbose")
if (is.null(x)) return(0L)
if (!is.atomic(x) && !is.data.frame(x))
stop("x must be an atomic vector or data.frames/data.tables")
if (is.atomic(x)) {
if (is.logical(x)) return(.Call(CuniqueNlogical, x, na.rm=na.rm))
if (is.logical(x)) {
# if (verbose) cat("uniqueN detected logical type, using optimised C uniqueNlogical\n")
return(.Call(CuniqueNlogical, x, na.rm=na.rm))
}
escape_n = 1e3L #getOption("datatable.escapeUniqueN",1e7L)
if (length(x) <= escape_n) {
#if (verbose) cat("uniqueN detected atomic type of length ", length(x), " which is small enough to use base R ", if (na.rm) "na.omit+" else "", "duplicated\n",sep="")
if (na.rm) x = na.omit(x)
return(length(x)-sum(duplicated(x)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably slightly quicker to not na.omit but just do:

return(length(x) - sum(duplicated(x)) - {na.rm && anyNA(x)})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not getting how that would work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If na.rm = FALSE then my version is just length(x) - sum(duplicated(x)) - 0.

If na.rm = TRUE then it will be length(x) - sum(duplicated(x)) - 1 * anyNA(x).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(key insight being that uniqueN can differ by at most 1 for na.rm vs !na.rm)

Copy link
Member Author

@jangorecki jangorecki Aug 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one!
it seems that it might eventually differ by 2

> x=c(1,2,NA,2,3,NaN,NaN,NA)
> length(unique(x))
[1] 5
> uniqueN(x)
[1] 5
> uniqueN(x, na.rm=T)
[1] 3
> length(unique(na.omit(x)))
[1] 3
> uniqueN(na.omit(x))
[1] 3

}
x = as_list(x)
}
if (is.null(by)) by = seq_along(x)
#if (verbose) cat("uniqueN proceeds using forderv\n",sep="")
o = forderv(x, by=by, retGrp=TRUE, na.last=if (!na.rm) FALSE else NA)
starts = attr(o, 'starts', exact=TRUE)
if (!na.rm) {
Expand Down