You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And not just lists but any type which is not supported by uniqueN could simply redirects to length(unique(.)).
library(data.table)
d1<- data.table(a=1:4, l=list(list(letters[1:2]),list(Sys.time()),list(1:10),list(letters[1:2])))
d1[,length(unique(l))]
# [1] 3d1[,uniqueN(l)]
#Error in uniqueN(l) : # x must be an atomic vector or data.frames/data.tables
Looks pretty easy to do, I will try to make PR for that.
This wouldn't work at the moment. Adding a check as to whether any column is of list type might be quite expensive when one performs a grouping operation (checking type for each group). And this is quite a rare case.. Hm.
Maybe for single column case we can already use the fix you've provided, and take care of .SD / data.table cases later. Will merge asap. Thanks @jangorecki.
This should not affect .SD as the check is if (!is.atomic(x) && !is.data.frame(x)) return(length(unique(x))).
data.table(a=list(1:2,4))[,is.data.frame(.SD)]
# [1] TRUE
data.table(a=list(1:2,4))[,uniqueN(a)]
# [1] 2
data.table(a=list(1:2,4))[,uniqueN(.SD)]
#Error in forderv(x, by = by, retGrp = TRUE) : # First column being ordered is type 'list', not yet supported
data.table(aa=1:2, a=list(1:2,4))[,uniqueN(.SD)]
# [1] 2
It still can throw error in a specific case of list column as first column, but this is related to #1229.
I will update doc, readme, and squash soon.
@arunsrinivasan I'm not sure if I follow. You are saying that list should be handled same as data.table? I don't think it is a proper behavior for list, while it is for data.table.
I can create uniqueN generic so it can be flexible.
Nope. I'm comfortable with uniqueN not supporting list-of-lists. It was not the original purpose. But it needs to do what forder() does for list of atomics (using mget() in j argument would not work as intended otherwise).
I will quote from source code, it is quite on point.
# simple straightforward helper function to get the number # of groups in a vector or data.table. Here by data.table, # we really mean `.SD` - used in a grouping operation
I was surprised to see a test for is.list(x) in the official usage of uniqueN, but x can't be a list list (though it's made clear in the Arguments section that this isn't handled).
Activity
arunsrinivasan commentedon Jul 16, 2015
@jangorecki this seems to do something different.
It seems to not check row-wise, rather if any element of list is identical to another.
jangorecki commentedon Jul 16, 2015
I used
uniqueN/length(unique(on single column while you did it on both columns. To make it more clear I simplified the example data:arunsrinivasan commentedon Jul 16, 2015
Okay. I guess the idea here is that a list is a vector as well.. that's fine.
Could you make sure it'd work on multiple columns as well? For example:
We'd want that to work, if someone did:
This wouldn't work at the moment. Adding a check as to whether any column is of
listtype might be quite expensive when one performs a grouping operation (checking type for each group). And this is quite a rare case.. Hm.arunsrinivasan commentedon Jul 16, 2015
Maybe for single column case we can already use the fix you've provided, and take care of
.SD/data.tablecases later. Will merge asap. Thanks @jangorecki.jangorecki commentedon Jul 16, 2015
This should not affect
.SDas the check isif (!is.atomic(x) && !is.data.frame(x)) return(length(unique(x))).It still can throw error in a specific case of list column as first column, but this is related to #1229.
I will update doc, readme, and squash soon.
uniqueN supports any types, solves Rdatatable#1224
Merge pull request #1225 from jangorecki/uniqueN_any
arunsrinivasan commentedon Mar 1, 2016
This fix returns wrong result for
list(1:2, 3:4).length(unique())should only be dispatched for list of lists.. not list of atomics.jangorecki commentedon Mar 1, 2016
@arunsrinivasan I'm not sure if I follow. You are saying that list should be handled same as data.table? I don't think it is a proper behavior for list, while it is for data.table.
I can create
uniqueNgeneric so it can be flexible.arunsrinivasan commentedon Mar 1, 2016
Nope. I'm comfortable with
uniqueNnot supportinglist-of-lists. It was not the original purpose. But it needs to do whatforder()does for list of atomics (usingmget()injargument would not work as intended otherwise).jangorecki commentedon Mar 1, 2016
I will quote from source code, it is quite on point.
arunsrinivasan commentedon Mar 1, 2016
That was done before your update to lists. It just wasn't updated. What's your point?
arunsrinivasan commentedon Mar 1, 2016
Will revisit later if the issue of lists come up. I'll revert this functionality to atomics / data.frames for now.
jangorecki commentedon Mar 1, 2016
@arunsrinivasan yes, just doing it now.
MichaelChirico commentedon Dec 11, 2016
A use case for this is in the syntax:
DT[ , uniqueN(.(var1, var2)), by = var3]Could be handled with
uniqueN(.SD)&.SDcols = c("var1", "var2")or withuniqueN(setDT(var1, var2)).The latter in particular pokes at:
uniqueNshould work at least fordata.table-likelists.I was also confused by the documentation, which has:
I was surprised to see a test for
is.list(x)in the official usage ofuniqueN, butxcan't be a list list (though it's made clear in the Arguments section that this isn't handled).