Vectors are one-dimensional arrays that can hold numeric data, character data, or logical data. The combine function c() is used to form the vector.
a <- c(1, 2, 5, 3, 6, -2, 4)
b <- c("one", "two", "three")
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
Here, a is numeric vector, b is a character vector, and c is a logical vector.
The data in a vector must only be one type or mode (numeric, character, or logical). We can’t mix modes in the same vector.
We can refer to elements of a vector using a numeric vector of positions within brackets.For example, a[c(2, 4)] refers to the 2nd and 4th element of vector a.
Asked In: Many Interviews |