Skip to content

Commit

Permalink
update arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-schwen committed Dec 7, 2023
1 parent 5e082b8 commit 442839e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
10 changes: 4 additions & 6 deletions R/transpose.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
transpose = function(l, fill=NA, ignore.empty=FALSE, keep.names=NULL, make.names=NULL, return.list=FALSE) {
transpose = function(l, fill=NA, ignore.empty=FALSE, keep.names=NULL, make.names=NULL, list.cols=FALSE) {
if (!is.null(make.names)) {
stopifnot(length(make.names)==1L)
if (is.character(make.names)) {
Expand All @@ -14,14 +14,12 @@ transpose = function(l, fill=NA, ignore.empty=FALSE, keep.names=NULL, make.names
colnames = as.character(l[[make.names]])
l = if (is.data.table(l)) l[,-make.names,with=FALSE] else l[-make.names]
}
ans = .Call(Ctranspose, l, fill, ignore.empty, keep.names, return.list)
ans = .Call(Ctranspose, l, fill, ignore.empty, keep.names, list.cols)
if (!is.null(make.names)) setattr(ans, "names", c(keep.names, colnames))
else if (is.data.frame(l)) # including data.table but not plain list
setattr(ans, "names", c(keep.names, paste0("V", seq_len(length(ans)-length(keep.names)))))
if (!return.list) {
if (is.data.table(l)) setDT(ans)
else if (is.data.frame(l)) setDF(ans)
}
if (is.data.table(l)) setDT(ans)
else if (is.data.frame(l)) setDF(ans)
ans[]
}

Expand Down
10 changes: 5 additions & 5 deletions src/transpose.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <Rdefines.h>
#include <time.h>

SEXP transpose(SEXP l, SEXP fill, SEXP ignoreArg, SEXP keepNamesArg, SEXP returnListArg) {
SEXP transpose(SEXP l, SEXP fill, SEXP ignoreArg, SEXP keepNamesArg, SEXP listColsArg) {

int nprotect=0;
if (!isNewList(l))
Expand All @@ -18,9 +18,9 @@ SEXP transpose(SEXP l, SEXP fill, SEXP ignoreArg, SEXP keepNamesArg, SEXP return
if (length(fill) != 1)
error(_("fill must be a length 1 vector, such as the default NA"));
R_len_t ln = LENGTH(l);
if (!isLogical(returnListArg) || LOGICAL(returnListArg)[0]==NA_LOGICAL)
error(_("ignore.empty should be logical TRUE/FALSE."));
bool returnList = LOGICAL(returnListArg)[0];
if (!isLogical(listColsArg) || LOGICAL(listColsArg)[0]==NA_LOGICAL)
error(_("list.cols should be logical TRUE/FALSE."));
bool listCol = LOGICAL(listColsArg)[0];

// preprocessing
int maxlen=0, zerolen=0;
Expand All @@ -36,7 +36,7 @@ SEXP transpose(SEXP l, SEXP fill, SEXP ignoreArg, SEXP keepNamesArg, SEXP return
if (isFactor(li)) type=STRSXP;
if (type>maxtype) maxtype=type;
}
if (returnList) maxtype=VECSXP; // need to keep preprocessing for zerolen
if (listCol) maxtype=VECSXP; // need to keep preprocessing for zerolen
fill = PROTECT(coerceVector(fill, maxtype)); nprotect++;

SEXP ans = PROTECT(allocVector(VECSXP, maxlen+rn)); nprotect++;
Expand Down

0 comments on commit 442839e

Please sign in to comment.