View | Details | Raw Unified | Return to bug 16640 | Differences between
and this patch

Collapse All | Expand All

(-)tapply.R (-17 / +15 lines)
Lines 20-43 Link Here
20
{
20
{
21
    FUN <- if (!is.null(FUN)) match.fun(FUN)
21
    FUN <- if (!is.null(FUN)) match.fun(FUN)
22
    if (!is.list(INDEX)) INDEX <- list(INDEX)
22
    if (!is.list(INDEX)) INDEX <- list(INDEX)
23
    nI <- length(INDEX)
23
    INDEX <- lapply(INDEX, as.factor)
24
    nI <- length(INDEX)  # now, 'INDEX' is not classed
24
    if (!nI) stop("'INDEX' is of length zero")
25
    if (!nI) stop("'INDEX' is of length zero")
25
    namelist <- vector("list", nI)
26
    if (!all(lengths(INDEX) == length(X)))
26
    names(namelist) <- names(INDEX)
27
        stop("arguments must have same length")
27
    extent <- integer(nI)
28
    namelist <- lapply(INDEX, levels)#- all of them, yes !
28
    nx <- length(X)
29
    extent <- lengths(namelist, use.names = FALSE)
29
    one <- 1L
30
    cumextent <- cumprod(extent)
30
    group <- rep.int(one, nx) #- to contain the splitting vector
31
    if (cumextent[nI] > .Machine$integer.max)
31
    ngroup <- one
32
        stop("total number of levels >= 2^31")
32
    for (i in seq_along(INDEX)) {
33
    storage.mode(cumextent) <- "integer"
33
	index <- as.factor(INDEX[[i]])
34
    ngroup <- cumextent[nI]
34
	if (length(index) != nx)
35
    group <- as.integer(INDEX[[1L]]) #- to contain the splitting vector
35
	    stop("arguments must have same length")
36
    if (nI > 1L)
36
	namelist[[i]] <- levels(index)#- all of them, yes !
37
        for (i in 2L:nI)
37
	extent[i] <- nlevels(index)
38
           group <- group + cumextent[i - 1L] * (as.integer(INDEX[[i]]) - 1L)
38
	group <- group + ngroup * (as.integer(index) - one)
39
	ngroup <- ngroup * nlevels(index)
40
    }
41
    if (is.null(FUN)) return(group)
39
    if (is.null(FUN)) return(group)
42
    ans <- lapply(X = split(X, group), FUN = FUN, ...)
40
    ans <- lapply(X = split(X, group), FUN = FUN, ...)
43
    index <- as.integer(names(ans))
41
    index <- as.integer(names(ans))

Return to bug 16640