Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# collapse 2.1.5.9000

* Fixed bug in `pivot(..., how = "wider", FUN = "sum")` (using internal sum function) when columns to aggregate were integer typed. Thanks @ummel (#803).

* Faster installation from source thanks to the `#include <Rcpp/Lighter>` option in *Rcpp* which loads only part of the header files. Thanks @eddelbuettel for the hint.

# collapse 2.1.5
Expand Down
8 changes: 4 additions & 4 deletions src/pivot.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ switch(aggfun) {
#define AGGFUN_SWITCH_NUM(tdef, TYPEACC, NONMISSCHECK, ISMISS) \
switch(aggfun) { \
case 4: { /* sum: no multithreading because possible race condition */ \
for(int i = 0; i != l; ++i) if(NONMISSCHECK) TYPEACC(pout[pid[i]])[pix[i]-1] += pc[i]; \
for(int i = 0; i != l; ++i) if(NONMISSCHECK) DBL_DATAPTR(pout[pid[i]])[pix[i]-1] += pc[i]; \
} break; \
case 5: { /* mean: no multithreading because possible race condition */ \
int *restrict count = (int*)R_Calloc(nr*nc+1, int); \
tdef *meani = TYPEACC(pout[1]); \
double *meani = DBL_DATAPTR(pout[1]); \
for(int i = 0; i != l; ++i) { \
if(NONMISSCHECK) { \
meani = TYPEACC(pout[pid[i]])-1; \
meani = DBL_DATAPTR(pout[pid[i]])-1; \
if(ISMISS(meani[pix[i]])) { \
meani[pix[i]] = pc[i]; \
++count[(pid[i]-1)*nr+pix[i]]; \
Expand Down Expand Up @@ -247,7 +247,7 @@ SEXP pivot_wide(SEXP index, SEXP id, SEXP column, SEXP fill, SEXP Rnthreads, SEX
if(aggfun < 3 || aggfun > 4) {
SEXP fill_val;
if(fill == R_NilValue || aggfun > 4) {
fill_val = tx == REALSXP ? ScalarReal(NA_REAL) : tx == INTSXP ? ScalarInteger(NA_INTEGER) :
fill_val = tx == REALSXP || aggfun == 5 ? ScalarReal(NA_REAL) : tx == INTSXP ? ScalarInteger(NA_INTEGER) :
tx == LGLSXP ? ScalarLogical(NA_LOGICAL) : tx == STRSXP ? ScalarString(NA_STRING) :
tx == CPLXSXP ? ScalarComplex(asComplex(ScalarReal(NA_REAL))) : tx == RAWSXP ? ScalarRaw(0) : R_NilValue;
} else if(TYPEOF(fill) == tx) {
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-miscellaneous-issues.R
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,29 @@ test_that("Misc bugs", {
})


test_that("Pivot with integers", { #803

iris_long <- pivot(iris, "Species")
iris_long$value <- round(iris_long$value) # Double
iris_long$value_int = as.integer(iris_long$value) # Integer

for (f in c("sum", "mean")) {
expect_equal(
pivot(
data = iris_long,
ids = "Species",
values = "value",
how = "wider", # Pivoting to wide format
FUN = "sum"
),
pivot(
data = iris_long,
ids = "Species",
values = "value_int",
how = "wider", # Pivoting to wide format
FUN = "sum"
))
}
})

options(warn = 1)