R/compact.R

Defines functions compact.ffdf compact.ff compact

Documented in compact compact.ff compact.ffdf

#' Compact a ff vector or ffdf data frame
#'
#' Compact takes a ff vector and tries to use the smallest binary data type for this vector.
#' @aliases compact compact.ff compact.ffdf
#' @method compact ff
#' @method compact ffdf
#' @export
#' @param x \code{ff} or \code{ffdf} object
#' @param use.na \code{logical} if TRUE the resulting ff vector can contain NA, otherwise this is not checked
#' @param ... other parameters
#' @return compact cloned ff vector, or original if no compacting can be done
compact <- function(x, use.na=TRUE, ...){
   UseMethod("compact")
}

#' @export
#' @export compact.ff
compact.ff <- function(x, use.na=TRUE,...){
   vm <- which(.vmode == vmode(x))[1]
   if (vm > 9){
     return(x)
   }
   
   idx <- seq_len(vm)
   if (ff::is.factor(x)){
     r <- c(1, nlevels(x))
   } else {
     r <- range(x, na.rm=TRUE)
   }
   
   m <- (r[1] >= .vmin) & (r[2] <= .vmax)
   if (isTRUE(use.na)){
     m <- m & is.na(.vNA)
   }
   
   m <- which(m[idx])[1]
   if (m < vm){
     ff:clone(x, vmode=.vmode[m])
   } else {
     x
   }
}

#' @export
#' @export compact.ffdf
compact.ffdf <- function(x, use.na=TRUE, ...){
   ret <- lapply(names(x), function(i){compact(x[[i]], use.na = use.na, ...)})
   res <- do.call(ffdf, ret)
   close(x)
   res
}

# # testing 1,2,3
# irisf <- as.ffdf(iris)
# irisc <- compact(irisf)
# vmode(irisc)

Try the ffbase package in your browser

Any scripts or data that you put into this service are public.

ffbase documentation built on Feb. 27, 2021, 5:06 p.m.