r - Unexpected result using unique inside a data.table -
given data.table (vith version 1.9.5)
test <- data.table(1:20,rep(1:5,each=4, times=1))
if run this:
test[unique(v2)]
i result:
v1 v2 1: 1 1 2: 2 1 3: 3 1 4: 4 1 5: 5 2
is intended beahaviour or bug? or i'm not using properly?
i reading "r book" , in example use test[unique(vegetation),] , it's intended select subset of rows unique vegetation.
i expected like
v1 v2 1: 1 1 2: 5 2 3: 9 3 4: 13 4 5: 16 5
though understand need specify aggregation criteria.
test[,unique(v2)]
gives [1] 1 2 3 4 5
. since test[1:5]
supposed give first 5 rows , that's get, there no bug.
to expected result, can this:
test[!duplicated(v2)] # v1 v2 #1: 1 1 #2: 5 2 #3: 9 3 #4: 13 4 #5: 17 5
or this:
test[, v1[1], = v2] # v2 v1 #1: 1 1 #2: 2 5 #3: 3 9 #4: 4 13 #5: 5 17
or @arun reminds me there data.table method unique
:
unique(test, by="v2") # v1 v2 #1: 1 1 #2: 5 2 #3: 9 3 #4: 13 4 #5: 17 5
Comments
Post a Comment