ID EN
Apply Functions

tapply

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Terapkan fungsi ke setiap sel array acak, yaitu ke setiap kelompok nilai (yang tidak kosong) yang diberikan oleh kombinasi unik dari tingkat faktor tertentu.

Syntax

R
tapply(X, INDEX, FUN = NULL, …, default = NA, simplify = TRUE)

Arguments

Parameter Deskripsi
X an R object for which a split method exists. Typically vector-like, allowing subsetting with [.
INDEX a list of one or more factors, each of same length as X. The elements are coerced to factors by as.factor.
FUN a function (or name of a function) to be applied, or NULL. In the case of functions like +, %*%, etc., the function name must be backquoted or quoted. If FUN is NULL, tapply returns a vector which can be used to subscript the multi-way array tapply normally produces.
… optional arguments to FUN: the Note section.
default (only in the case of simplification to an array) the value with which the array is initialized as array(default, dim = ..). Before R 3.4.0, this was hard coded to array()'s default NA. If it is NA (the default), the missing value of the answer type, e.g. NA_real_, is chosen (as.raw(0) for "raw"). In a numerical case, it may be set, e.g., to FUN(integer(0)), e.g., in the case of FUN = sum to 0 or 0L.
simplify logical; if FALSE, tapply always returns an array of mode "list"; in other words, a list with a dim attribute. If TRUE (the default), then if FUN always returns a scalar, tapply returns an array with the mode of the scalar.

Return Value

Ketika FUN hadir, tapply memanggil FUN untuk setiap sel yang memiliki data apa pun di dalamnya. Jika FUN mengembalikan nilai atom tunggal untuk setiap sel tersebut (misalnya, fungsi mean atau var) dan ketika penyederhanaan adalah TRUE, tapply mengembalikan array multi-arah yang berisi nilai, dan NA untuk sel kosong. Array memiliki jumlah dimensi yang sama dengan komponen INDEX; jumlah level dalam suatu dimensi adalah jumlah level (nlevels()) pada komponen INDEX yang bersangkutan. Perhatikan bahwa jika nilai yang dikembalikan memiliki kelas (mis

Details

Jika FUN bukan NULL, maka diteruskan ke match.fun, dan karenanya dapat berupa fungsi atau simbol atau string karakter yang memberi nama suatu fungsi.

Contoh

Example
R
# NOT RUN {
require(stats)
groups <- as.factor(rbinom(32, n = 5, prob = 0.4))
tapply(groups, groups, length) #- is almost the same as
table(groups)

## contingency table from data.frame : array with named dimnames
tapply(warpbreaks$breaks, warpbreaks[,-1], sum)
tapply(warpbreaks$breaks, warpbreaks[, 3, drop = FALSE], sum)

n <- 17; fac <- factor(rep_len(1:3, n), levels = 1:5)
table(fac)
tapply(1:n, fac, sum)
tapply(1:n, fac, sum, default = 0) # maybe more desirable
tapply(1:n, fac, sum, simplify = FALSE)
tapply(1:n, fac, range)
tapply(1:n, fac, quantile)
tapply(1:n, fac, length) ## NA's
tapply(1:n, fac, length, default = 0) # == table(fac)
# }
# NOT RUN {
## example of ... argument: find quarterly means
tapply(presidents, cycle(presidents), mean, na.rm = TRUE)

ind <- list(c(1, 2, 2), c("A", "A", "B"))
table(ind)
tapply(1:3, ind) #-> the split vector
tapply(1:3, ind, sum)

## Some assertions (not held by all patch propsals):
nq <- names(quantile(1:5))
stopifnot(
  identical(tapply(1:3, ind), c(1L, 2L, 4L)),
  identical(tapply(1:3, ind, sum),
            matrix(c(1L, 2L, NA, 3L), 2, dimnames = list(c("1", "2"), c("A", "B")))),
  identical(tapply(1:n, fac, quantile)[-1],
            array(list(`2` = structure(c(2, 5.75, 9.5, 13.25, 17), .Names = nq),
                 `3` = structure(c(3, 6, 9, 12, 15), .Names = nq),
                 `4` = NULL, `5` = NULL), dim=4, dimnames=list(as.character(2:5)))))
# }

See Also

the convenience functions by and aggregate (using tapply); apply lapply with its versions sapply and mapply.