ID EN
Vector & List

Vectorize

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Vectorize membuat pembungkus fungsi yang memvektorisasi aksi argumennya FUN.

Syntax

R
Vectorize(FUN, vectorize.args = arg.names, SIMPLIFY = TRUE,
          USE.NAMES = TRUE)

Arguments

Parameter Deskripsi
FUN function to apply, found via match.fun.
vectorize.args a character vector of arguments which should be vectorized. Defaults to all arguments of FUN.
SIMPLIFY logical or character string; attempt to reduce the result to a vector, matrix or higher dimensional array; see the simplify argument of sapply.
USE.NAMES logical; use names if the first … argument has names, or if it is a character vector, use that character vector as the names.

Return Value

Sebuah fungsi dengan argumen yang sama dengan FUN, mengakhiri panggilan ke mapply.

Details

Argumen yang disebutkan dalam argumen vectorize.args ke Vectorize adalah argumen yang diteruskan dalam daftar ... ke mapply. Hanya yang benar-benar lolos yang akan divektorisasi; nilai default tidak akan. Lihat contohnya. Vektorisasi tidak dapat digunakan dengan fungsi primitif karena tidak memiliki nilai formal. Ini juga tidak dapat digunakan dengan fungsi yang memiliki argumen bernama FUN, vectorize.args, SIMPLIFY, atau USE.NAMES, karena akan mengganggu argumen Vectorize. Lihat contoh kombinasi di bawah untuk solusinya.

Contoh

Example
R
# NOT RUN {
# We use rep.int as rep is primitive
vrep <- Vectorize(rep.int)
vrep(1:4, 4:1)
vrep(times = 1:4, x = 4:1)

vrep <- Vectorize(rep.int, "times")
vrep(times = 1:4, x = 42)

f <- function(x = 1:3, y) c(x, y)
vf <- Vectorize(f, SIMPLIFY = FALSE)
f(1:3, 1:3)
vf(1:3, 1:3)
vf(y = 1:3) # Only vectorizes y, not x

# Nonlinear regression contour plot, based on nls() example
require(graphics)
SS <- function(Vm, K, resp, conc) {
    pred <- (Vm * conc)/(K + conc)
    sum((resp - pred)^2 / pred)
}
vSS <- Vectorize(SS, c("Vm", "K"))
Treated <- subset(Puromycin, state == "treated")

Vm <- seq(140, 310, length.out = 50)
K <- seq(0, 0.15, length.out = 40)
SSvals <- outer(Vm, K, vSS, Treated$rate, Treated$conc)
contour(Vm, K, SSvals, levels = (1:10)^2, xlab = "Vm", ylab = "K")

# combn() has an argument named FUN
combnV <- Vectorize(function(x, m, FUNV = NULL) combn(x, m, FUN = FUNV),
                    vectorize.args = c("x", "m"))
combnV(4, 1:4)
combnV(4, 1:4, sum)
# }