Skip to content

Commit

Permalink
Closes #1023. Preserve attributes in gsum and gmean.
Browse files Browse the repository at this point in the history
  • Loading branch information
arunsrinivasan committed Jan 27, 2015
1 parent a18d624 commit bc6c45b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

9. `rbindlist` gains `idcol` argument which can be used to generate an index column. If `idcol=TRUE`, the column is automatically named `.id`. Instead you can also provide a column name directly. If the input list has no names, indices are automatically generated. Closes [#591](https://github.com/Rdatatable/data.table/issues/591). Also thanks to @KevinUshey for filing [#356](https://github.com/Rdatatable/data.table/issues/356).

10. A new helper function `uniqueN` is now implemented. It is equivalent to `length(unique(x))` but much faster. It accepts `atomic vectors`, `data.frames` and `data.tables` as input and returns the number of unique rows. For example, DT[, .(uN = uniqueN(.SD)), by=x]` returns the number of unique rows within each group of `x`.
10. A new helper function `uniqueN` is now implemented. It is equivalent to `length(unique(x))` but much faster. It accepts `atomic vectors`, `data.frames` and `data.tables` as input and returns the number of unique rows. For example, DT[, .(uN = uniqueN(.SD)), by=x]` returns the number of unique rows within each group of `x`. Thanks to @DavidArenburg as well for the FR.

This comment has been minimized.

Copy link
@DavidArenburg

DavidArenburg Jan 29, 2015

Member

You need to add an additional back tick before DT[, .(uN = uniqueN(.SD)), by=x]

This comment has been minimized.

Copy link
@arunsrinivasan

arunsrinivasan Jan 30, 2015

Author Member

A good time to try that PR :-).


#### BUG FIXES

Expand Down Expand Up @@ -113,6 +113,8 @@

29. Fixed two segfaults in `shift()` when number of rows in `x` is lesser than value for `n`. Closes [#1009](https://github.com/Rdatatable/data.table/issues/1009) and [#1014](https://github.com/Rdatatable/data.table/issues/1014). Thanks to @jangorecki and @ashinm for the reproducible reports.

30. Attributes are preserved for `sum()` and `mean()` when fast internal (GForce) implementations are used. Closes [#1023](https://github.com/Rdatatable/data.table/issues/1023). Thanks to @DavidArenburg for the nice reproducible example.

#### NOTES

1. Clearer explanation of what `duplicated()` does (borrowed from base). Thanks to @matthieugomez for pointing out. Closes [#872](https://github.com/Rdatatable/data.table/issues/872).
Expand Down
4 changes: 4 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -5869,6 +5869,10 @@ DT <- data.table(A = rep(1:3, each=4), B = rep(1:4, each=3), C = rep(1:2, 6))
test(1475.1, uniqueN(DT), 10L)
test(1475.2, DT[, .(uN=uniqueN(.SD)), by=A], data.table(A=1:3, uN=c(3L,4L,3L)))

# preserve class attribute in GForce mean (and sum)
DT <- data.table(x = rep(1:3, each = 3), y = as.Date(seq(Sys.Date(), (Sys.Date() + 8), by = "day")))
test(1476.1, DT[, .(y=mean(y)), x], setDT(aggregate(y ~ x, DT, mean)))

##########################


Expand Down
2 changes: 2 additions & 0 deletions src/gsumm.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ SEXP gsum(SEXP x, SEXP narm)
error("Type '%s' not supported by GForce sum (gsum). Either add the prefix base::sum(.) or turn off GForce optimization using options(datatable.optimize=1)", type2char(TYPEOF(x)));
}
free(s);
copyMostAttrib(x, ans);
UNPROTECT(1);
// Rprintf("this gsum took %8.3f\n", 1.0*(clock()-start)/CLOCKS_PER_SEC);
return(ans);
Expand Down Expand Up @@ -163,6 +164,7 @@ SEXP gmean(SEXP x, SEXP narm)
else REAL(ans)[i] = (double)s[i];
}
free(s); free(c);
copyMostAttrib(x, ans);
UNPROTECT(1);
// Rprintf("this gmean na.rm=TRUE took %8.3f\n", 1.0*(clock()-start)/CLOCKS_PER_SEC);
return(ans);
Expand Down

0 comments on commit bc6c45b

Please sign in to comment.