Vectorized operations are operations that occur in parallel in certain R objects. This allows you to write code that is efficient, concise, and easier to read than in non-vectorized languages.
For example, the following code adds two vectors together:
x <- 1:4
y <- 6:9
z <- x + y
This code is vectorized, so the addition operation is performed on each element of the vectors x and y, and the result is a new vector z.
Another example of a vectorized operation is a logical comparison. The following code checks which elements of a vector are greater than 2:
x <- 1:4
x > 2
This code returns a logical vector of TRUE and FALSE, indicating which elements of x are greater than 2.
Vectorized operations can be used with a wide variety of R objects, including vectors, matrices, and data frames. They are a powerful tool that can help you write efficient and concise code.
Examples of vectorized operations
- `x * y` multiplies each element of x by each element of y.
- `x / y` divides each element of x by each element of y.
- `x ^ y` raises each element of x to the power of each element of y.
- `abs(x)` returns the absolute value of each element of x.
- `log(x)` returns the natural logarithm of each element of x.