r add over vector code example
Example 1: apply on a vector in r
lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.
sapply is a “user-friendly” version of lapply by default returning a vector or matrix if appropriate.
replicate is a wrapper for the common use of sapply for repeated evaluation of an expression (which will usually involve random number generation).
Usage
lapply(X, FUN, ...)
sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
replicate(n, expr, simplify = TRUE)
Example 2: R for loop append to vector
for(i in 1:3) { # Head of for-loop
new_value <- i * (- 1) # Creating new value
my_vec <- c(my_vec, new_value) # Appending new value to vector
}