Creating Vectors in R Using
c() function can be used to create vectors of objects. This function concatenates the values having one dimension (either row or column matrix in a sense). The following are some examples related to creating different types of vectors in R.
# Numeric vector x <- c(1, 2, 5, 0.5, 10, 20, pi) # Logical vector x <- c(TRUE, FALSE, FALSE, T, T, F) # Character vector x <- c("a", "z", "good", "bad", "null hypothesis") # Integer vector x <- 9 : 29 # (colon operator is used) x <- c(1L, 5L, 0L, 15L) # Complex vector x <- c(1+0i, 2+4i, 0+0i)
Creates a vector of $n$ elements with a default value of zero for numeric vector, an empty string for character vector, FALSE for logical vector, and 0+0i for complex vector.
# Numeric vector of lenght 10 (default is zero) x <- vector("numeric", length = 10) # Integer vector of length 10 (default is integer zeros) x <- vector("integer", length = 10) # Character vector of length 10 (default is empty string) x <- vector("character", length = 10) # Logical vector of length 10 (default is FALSE) x <- vector("logical", length = 10) # Complex vector of length 10 (default is 0+0i) x<- vector("complex", length=10)
Creating Vectors with Mixed objects
When different objects are mixed in a vector, coercion occurs, that is, the data type of vector changes intelligently.
The following are examples
# coerce to character vector y <- c(1.2, "good") y <- c("a", T) # coerce to a numeric vector y <- c(T, 2)
From the above examples, the coercion will make each element of the vector of the same class.
Explicitly Coercing Objects to Other Class
Objects can be explicitly coerced from one class to another class using
as.logical() functions. For example;
x <- 0:6 as.numeric(x) as.logical(x) as.character(x) as.complex(x)
Note that non-sensual coercion results in NAs (missing values). For example,
x <- c("a", "b", "c") as.numeric(x) as.logical(x) as.complex(x) as.integer(x)
Vectorization in R
Many operations in R Language are vectorized. The operations ( +, -, *, and / ) are performed element by element. For example,
x <- 1 : 4 y <- 6 : 9 x + y x - y x * y x / y x >= 2 x < 3 y == 8
Without vectorization (as in other languages) one has to use for loop for performing element by element operation on say vectors.
Subsetting Vectors in R Language
By subsetting vectors means that extracting the elements of a vector. For this purpose square brackets ([ ]) are used. For example;
x <- c(1, 6, 10, -15, 0, 13, 5, 2, 10, 9) # Subsetting Examples x # extract first element of x vecotr x[1:5] # extract first five values of x x[-1] # extract all values except first x[x > 2] # extracts all elements that are greater than 2 head(x) # extracts first 6 elements of x tail(x) # extracts last 6 elements of x x[x > 5 & x < 10] # extracts elements that are greater than 5 but less than 10
One can use subset() function to extract the desired element using logical operators, For example,
subset(x, x > 5) subset(x, x > 5 & x < 10) subset(x, !x < 0 )