diff --git a/R/setkey.R b/R/setkey.R index 01726b3e2..9d1372f6b 100644 --- a/R/setkey.R +++ b/R/setkey.R @@ -307,7 +307,78 @@ CJ = function(..., sorted = TRUE) l } - +frankv = function(x, na.last=TRUE, order=1L, ties.method=c("average", "first", "random", "max", "min")) { + ties.method = match.arg(ties.method) + na.last = as.logical(na.last) + if (!length(na.last)) stop('length(na.last) = 0') + if (length(na.last) != 1L) { + warning("length(na.last) > 1, only the first element will be used") + na.last = na.last[1L] + } + as_list <- function(x) { + xx = vector("list", 1L) + .Call("Csetlistelt", xx, 1L, x) + xx + } + if (is.atomic(x)) x = as_list(x) + else { + n = vapply(x, length, 0L) + if (any(n +// #include // the debugging machinery + breakpoint aidee +// raise(SIGINT); + +extern SEXP char_integer64; + +static union { + double d; + unsigned long long ull; +} u; + +SEXP dt_na(SEXP x, SEXP xorderArg, SEXP xstartArg) { + int i, j, k, n = length(xstartArg), *xstart = INTEGER(xstartArg), *xorder = INTEGER(xorderArg); + SEXP v, ans, class; + double *dv; + + if (!isNewList(x)) error("Internal error: 'x' should be a list. Please report to datatable-help"); + ans = PROTECT(allocVector(LGLSXP, n)); + for (i=0; i 0) { + switch (ties) { + case MEAN : + for (i = 0; i < length(xstartArg); i++) { + for (j = xstart[i]-1; j < xstart[i]+xlen[i]-1; j++) + REAL(ans)[xorder[j]-1] = (2*xstart[i]+xlen[i]-1)/2.0; + } + break; + case MAX : + for (i = 0; i < length(xstartArg); i++) { + for (j = xstart[i]-1; j < xstart[i]+xlen[i]-1; j++) + INTEGER(ans)[xorder[j]-1] = xstart[i]+xlen[i]-1; + } + break; + case MIN : + for (i = 0; i < length(xstartArg); i++) { + for (j = xstart[i]-1; j < xstart[i]+xlen[i]-1; j++) + INTEGER(ans)[xorder[j]-1] = xstart[i]; + } + break; + } + } + UNPROTECT(1); + return(ans); +} diff --git a/src/init.c b/src/init.c index 7ea6bbdab..8971c14af 100644 --- a/src/init.c +++ b/src/init.c @@ -49,6 +49,8 @@ SEXP chmatch2(); SEXP subsetDT(); SEXP subsetVector(); SEXP convertNegativeIdx(); +SEXP frank(); +SEXP dt_na(); // .Externals SEXP fastmean(); @@ -100,6 +102,8 @@ R_CallMethodDef callMethods[] = { {"CsubsetDT", (DL_FUNC) &subsetDT, -1}, {"CsubsetVector", (DL_FUNC) &subsetVector, -1}, {"CconvertNegativeIdx", (DL_FUNC) &convertNegativeIdx, -1}, +{"Cfrank", (DL_FUNC) &frank, -1}, +{"Cdt_na", (DL_FUNC) &dt_na, -1}, {NULL, NULL, 0} };