Repository navigation
Closed
Labels
Milestone
Description
Did a bunch of coalesceing today and was sorely missing an efficient version.
@HughParsonage, would you be happy to add hutils::coalesce to data.table? I have the discussion in #2677 in mind...
I made tinkered with hutils::coalesce to come up with:
coalesce <- function(x, ...) {
if (!any(na_idx <- is.na(x)) || missing(..1)) return(x)
values <- list(...)
lx <- length(x)
lengths <- c(lx, vapply(values, length, FUN.VALUE = 0L))
lengthsn1 <- lengths != 1L
if (any(lengthsn1 & lengths != lx)) {
wrong_len_i <- which(lengthsn1 & lengths != lx)
stop("Argument ", wrong_len_i[1], " had length ", lengths[wrong_len_i[1]], ", ",
"but length(x) = ", lx, ". ",
"The only permissible lengths in ... are 1 or the length of `x` (", lx, ").")
}
typeof_x <- typeof(x)
x_not_factor <- !inherits(x, what = 'factor')
lv <- length(values)
for (i in seq_len(lv)) {
vi <- values[[i]]
if (typeof(vi) != typeof_x) {
stop("Argument ", i + 1L, " had type '", typeof(vi), "' but ",
"typeof(x) was ", typeof_x, ". All types ",
"in `...` must be the same type.")
}
if (inherits(vi, what = "factor") && x_not_factor) {
stop("Argument ", i + 1L, " was a factor, but `x` was not. ",
"All `...` must be the same type.")
}
if (lengthsn1[i + 1L]) {
has_value_idx <- !is.na(vi[na_idx])
x[na_idx][has_value_idx] <- vi[na_idx][has_value_idx]
if (all(has_value_idx)) break
na_idx[has_value_idx] = FALSE
} else {
if (is.na(vi)) next
else x[na_idx] <- vi
break
}
}
x
}
main difference being to skip running anyNA every iteration and instead focus on "whittling down" the is.na(x) vector
Benchmarked against hmisc::coalesce and it's hit or miss... maybe need more replications (function evaluation takes at most around 2 seconds so this is doable)? Or there's some extra optimization I'm missing...
Anyway, the same logic would probably be faster in C...
Reacted by Sean Ingerson, Jan Gorecki, Uwe Block and Matt Dowle
Activity
HughParsonage commentedon Feb 20, 2019
You'll need to go back to GPL-2 for this.
HughParsonage commentedon Feb 20, 2019
Otherwise happy of course.
For the optimization, you'll need to go down to C to do better.
anyNAis so much faster thanis.nathat it's only in perverse circumstances whereany(is.na(v[v_not_na]))is going to outperform it (like where...length()is large and theNAs are rare and at the bottom of all of them).I've got 128 GB and I've never had
hutils::coalescebe a bottleneck.jangorecki commentedon Feb 20, 2019
I used
coalescea lot of in SQL. I would say it is worth to write it in C.smingerson commentedon Mar 2, 2019
This would be an excellent addition to data.table. Do you think it would be possible to support
patterns(), similar to functionality suggested in #3396 ? Frequently, I have columns which come from sources of varying reliability. I'll have 20 columns from each of 3+ sources, and want to coalesce values in order of reliability.It would take a vector (or list) of patterns, find matches, then group the matches by the remaining portion of the name. It would coalesce each group of matches in the order the patterns were provided. I think this would be very convenient functionality, but not quite sure how it would work in the j-slot. I also may be overlooking a simple way to do this using the proposed version of coalesce.
jangorecki commentedon May 29, 2019
@smingerson these kind of interface can be easily and reliably handled on user side.
I generally agree it would be useful to provide alternative interface to pass
...argument, for example as a list.So I pushed improvement for that...
and because you can use
patternshelper in.SDcols, you can easily replaceyour_pattern_funfrom above example with data.tablepatternshelper.add alt interface for dots to coalesce, as requested in #3424
[-]FR: data.table-style coalesce[/-][+]data.table-style coalesce[/+]mattdowle commentedon Jun 17, 2019
@HughParsonage
Please note https://www.gnu.org/licenses/gpl-faq.en.html#IfInterpreterIsGPL which contains "The interpreted program, to the interpreter, is just data.". See also my comments about that GPL FAQ at the top of PR #2456 which changed data.table's license from GPL to MPL.
In short: my understanding is that the license of any R-only package (such as your
hutils) is not relevant because the R code is considered data by the GPL, not code. According to the official GPL FAQ.Of course, on the data.table project we will respect your intent. I'm just saying what I believe the license situation to be.
mattdowle commentedon Jul 17, 2019
PR #3696 added complex support to coalesce, part of #3690.