Applies a function by every n values to a vector.
this_by_n(invec, n = 3, FUN = sum, fill = NA, include_first = TRUE)
invec | The input vector. |
---|---|
n | By how many values? |
FUN | The function to apply to each set of n values. |
fill | The value to padd the resulting vector with. Defaults to |
include_first | Logical. Should the first value be included. Defaults to |
A vector the same length as the input vector.
http://stackoverflow.com/q/34563693/1270695
Ananda Mahto
#> [1] 2.000000 3.000000 4.666667 6.666667 6.000000 5.000000 3.000000 4.000000this_by_n(x, 2, max)#> [1] 2 3 4 7 9 9 4 4this_by_n(x, 4, min)#> [1] 1 2 3 2 2 2 2 4this_by_n(letters[1:10], 5, toString)#> [1] "a, b, c, d, e" "b, c, d, e, f" "c, d, e, f, g" "d, e, f, g, h" #> [5] "e, f, g, h, i" "f, g, h, i, j" "g, h, i, j" "h, i, j" #> [9] "i, j" "j"