define vector in r code example

Example 1: r create a vector

# Basic syntax:
c(11, 23, 42) # Vector of numerics
c(TRUE, FALSE, TRUE) # Vector of booleans/logicals
c("aa", "bb", "cc") # Vector of strings

# Note, in R, list and vectors (aka atomic vectors) are both 
#	one-dimensional objects, but lists can contain mixed type data 
#	whereas vectors can only contain one type of data
# Note, to make a list, use: list(11, 23, 42)

Example 2: 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)

Tags:

Misc Example