ID EN
Miscellaneous

funprog

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Reduce menggunakan fungsi biner untuk menggabungkan elemen vektor tertentu dan kemungkinan nilai awal tertentu secara berturut-turut. Filter mengekstrak elemen vektor yang fungsi predikatnya (logis) memberikan nilai true. Temukan dan Posisi masing-masing memberikan elemen pertama atau terakhir serta posisinya dalam vektor. Peta menerapkan fungsi ke elemen-elemen yang bersesuaian dari vektor-vektor tertentu. Negate menciptakan negasi dari fungsi tertentu.

Syntax

R
Reduce(f, x, init, right = FALSE, accumulate = FALSE)
Filter(f, x)
Find(f, x, right = FALSE, nomatch = NULL)
Map(f, ...)
Negate(f)
Position(f, x, right = FALSE, nomatch = NA_integer_)

Arguments

Parameter Deskripsi
f a function of the appropriate arity (binary for Reduce, unary for Filter, Find and Position, \(k\)-ary for Map if this is called with \(k\) arguments). An arbitrary predicate function for Negate.
x a vector.
init an R object of the same kind as the elements of x.
right a logical indicating whether to proceed from left to right (default) or from right to left.
accumulate a logical indicating whether the successive reduce combinations should be accumulated. By default, only the final combination is used.
nomatch the value to be returned in the case when “no match” (no element satisfying the predicate) is found.
… vectors.

Details

Jika init diberikan, Reduce secara logis menambahkannya ke awal (saat melanjutkan dari kiri ke kanan) atau akhir x. Jika vektor yang mungkin diperbesar \(v\) ini memiliki elemen \(n > 1\), Pengurangan secara berturut-turut menerapkan \(f\) ke elemen \(v\) dari kiri ke kanan atau kanan ke kiri. Yaitu, pengurangan kiri menghitung \(l_1 = f(v_1, v_2)\), \(l_2 = f(l_1, v_3)\), dll., dan mengembalikan \(l_{n-1} = f(l_{n-2}, v_n)\), dan pengurangan kanan menghasilkan \(r_{n-1} = f(v_{n-1}, v_n)\), \(r_{n-2} = f(v_{n-2}, r_{n-1})\) dan mengembalikan \(r_1 = f(v_1, r_2)\). (Misalnya, jika \(v\) adalah barisan (2, 3, 4) dan \(f\) adalah pembagian, pengurangan kiri dan kanan menghasilkan \((2 / 3) / 4 = 1/6\) dan \(2 / (3 / 4) = 8/3\), masing-masing.) Jika \(v\) hanya memiliki satu elemen, ini dikembalikan; jika tidak ada elemen, NULL dikembalikan. Dengan demikian,

Contoh

Example
R
# NOT RUN {
## A general-purpose adder:
add <- function(x) Reduce("+", x)
add(list(1, 2, 3))
## Like sum(), but can also used for adding matrices etc., as it will
## use the appropriate '+' method in each reduction step.
## More generally, many generics meant to work on arbitrarily many
## arguments can be defined via reduction:
FOO <- function(...) Reduce(FOO2, list(...))
FOO2 <- function(x, y) UseMethod("FOO2")
## FOO() methods can then be provided via FOO2() methods.

## A general-purpose cumulative adder:
cadd <- function(x) Reduce("+", x, accumulate = TRUE)
cadd(seq_len(7))

## A simple function to compute continued fractions:
cfrac <- function(x) Reduce(function(u, v) u + 1 / v, x, right = TRUE)
## Continued fraction approximation for pi:
cfrac(c(3, 7, 15, 1, 292))
## Continued fraction approximation for Euler's number (e):
cfrac(c(2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8))

## Iterative function application:
Funcall <- function(f, ...) f(...)
## Compute log(exp(acos(cos(0))))
Reduce(Funcall, list(log, exp, acos, cos), 0, right = TRUE)
## n-fold iterate of a function, functional style:
Iterate <- function(f, n = 1)
    function(x) Reduce(Funcall, rep.int(list(f), n), x, right = TRUE)
## Continued fraction approximation to the golden ratio:
Iterate(function(x) 1 + 1 / x, 30)(1)
## which is the same as
cfrac(rep.int(1, 31))
## Computing square root approximations for x as fixed points of the
## function t |-> (t + x / t) / 2, as a function of the initial value:
asqrt <- function(x, n) Iterate(function(t) (t + x / t) / 2, n)
asqrt(2, 30)(10) # Starting from a positive value => +sqrt(2)
asqrt(2, 30)(-1) # Starting from a negative value => -sqrt(2)

## A list of all functions in the base environment:
funs <- Filter(is.function, sapply(ls(baseenv()), get, baseenv()))
## Functions in base with more than 10 arguments:
names(Filter(function(f) length(formals(f)) > 10, funs))
## Number of functions in base with a '...' argument:
length(Filter(function(f)
              any(names(formals(f)) %in% "..."),
              funs))
# }
# NOT RUN {
## Find all objects in the base environment which are *not* functions:
Filter(Negate(is.function),  sapply(ls(baseenv()), get, baseenv()))
# }

See Also

Function clusterMap and mcmapply (not Windows) in package parallel provide parallel versions of Map.