You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pbtapply <-
function (X, INDEX, FUN = NULL, ..., simplify = TRUE, cl = NULL) # changed here
{
FUN <- if (!is.null(FUN))
match.fun(FUN)
if (!is.list(INDEX))
INDEX <- list(INDEX)
INDEX <- lapply(INDEX, as.factor)
nI <- length(INDEX)
if (!nI)
stop("'INDEX' is of length zero")
if (!all(lengths(INDEX) == length(X)))
stop("arguments must have same length")
namelist <- lapply(INDEX, levels)
extent <- lengths(namelist, use.names = FALSE)
cumextent <- cumprod(extent)
if (cumextent[nI] > .Machine$integer.max)
stop("total number of levels >= 2^31")
storage.mode(cumextent) <- "integer"
ngroup <- cumextent[nI]
group <- as.integer(INDEX[[1L]])
if (nI > 1L)
for (i in 2L:nI) group <- group + cumextent[i - 1L] *
(as.integer(INDEX[[i]]) - 1L)
if (is.null(FUN))
return(group)
levels(group) <- as.character(seq_len(ngroup))
class(group) <- "factor"
ans <- split(X, group)
names(ans) <- NULL
index <- as.logical(lengths(ans))
ans <- pblapply(X = ans[index], FUN = FUN, cl=cl, ...) # changed here
if (simplify && all(lengths(ans) == 1L)) {
ansmat <- array(dim = extent, dimnames = namelist)
ans <- unlist(ans, recursive = FALSE)
}
else {
ansmat <- array(vector("list", prod(extent)), dim = extent,
dimnames = namelist)
}
if (length(ans)) {
ansmat[index] <- ans
}
ansmat
}
Some examples:
library(pbapply)
require(stats)
groups <- as.factor(rbinom(32, n = 5, prob = 0.4))
pbtapply(groups, groups, length) #- is almost the same as
table(groups)
## contingency table from data.frame : array with named dimnames
pbtapply(warpbreaks$breaks, warpbreaks[,-1], sum)
pbtapply(warpbreaks$breaks, warpbreaks[, 3, drop = FALSE], sum)
n <- 17; fac <- factor(rep(1:3, length = n), levels = 1:5)
table(fac)
pbtapply(1:n, fac, sum)
pbtapply(1:n, fac, sum, simplify = FALSE)
pbtapply(1:n, fac, range)
pbtapply(1:n, fac, quantile)
## example of ... argument: find quarterly means
pbtapply(presidents, cycle(presidents), mean, na.rm = TRUE)
ind <- list(c(1, 2, 2), c("A", "A", "B"))
table(ind)
pbtapply(1:3, ind) #-> the split vector
pbtapply(1:3, ind, sum)
The text was updated successfully, but these errors were encountered:
Adapt
base::tapply
function aspbtapply
.Some examples:
The text was updated successfully, but these errors were encountered: