what is Vector in R:
In this tutorial we will learn about vector in R. The vector is the simplest way to store more than one value in R. The c function (mnemonic for concatenate or combine) allows you to quickly enter data into R.
Numeric Vector Example:
X <- c(1,-2,5.3,6,-20,4) # numeric vector print(X)
Output:
Character Vector Example:
Y <- c("one","two","three") # character vector print(Y)
Output:
Logical Vector Example:
Z <- c(FALSE,TRUE,FALSE,FALSE,TRUE,FALSE) #logical vector print(Z)
Output:
In vector, Objects should be of single data type. vector doesn’t accept multiple data type
Example :
W <- c(FALSE,1,-2.0,"two") print(W) mode(W)
Output:
[1] “FALSE” “1” “-2” “two”
[1] “character”
In the above example, the object W has converted all the elements of different data type into character.
Accessing Vector Elements in R
Elements of a Vector in R are accessed using indexing. The [ ] brackets are used for indexing. Indexing starts with position 1. Giving a negative value in the index drops the element of that position from result. TRUE, FALSE or 0 and 1 can also be used for indexing.
# Accessing vector elements using position. x <- c("Jan","Feb","Mar","April","May","June","July") y <- x[c(2,3,6)] print(y) # Accessing vector elements using logical indexing. v <- x[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)] print(v) # Accessing vector elements using negative indexing. t <- x[c(-2,-5)] print(t)
When we execute the above code, it produces the following result −
[1] “Feb” “Mar” “June”
[1] “Jan” “June”
[1] “Jan” “Mar” “April” “June” “July”
Vector arithmetic operations(addition subtraction, multiplication and division) in R:
Two vectors of same length can be added, subtracted, multiplied or divided giving the result as a vector output.
# Create two vectors. v1 <- c(1,2,4,5,7,11) v2 <- c(12,4,3,8,1,21) # Vector addition. add.result <- v1 v2 print(add.result) # Vector substraction. sub.result <- v1-v2 print(sub.result) # Vector multiplication. multi.result <- v1*v2 print(multi.result) # Vector division. divi.result <- v1/v2 print(divi.result)
When we execute the above code, it produces the following result −
[1] 13 6 7 13 8 32
[1] -11 -2 1 -3 6 -10
[1] 12 8 12 40 7 231
[1] 0.08333333 0.50000000 1.33333333 0.62500000 7.00000000 0.52380952
Vector element recycling in R
If we apply arithmetic operations to two vectors of unequal length, then the elements of the shorter vector are recycled to complete the operations.
v1 <- c(1,2,4,5,7,11) v2 <- c(4,11) # V2 becomes c(4,11,4,11,4,11) add.result <- v1 v2 print(add.result) sub.result <- v1-v2 print(sub.result)
When we execute the above code, it produces the following result −
[1] 5 13 8 16 11 22
[1] -3 -9 0 -6 3 0
Sorting a Vector in R
Elements in a vector can be sorted using the sort() function.
#numerical vector sort v1 <- c(1,2,4,5,7,11) sort(v1) # sorts in ascending order which is default sort(v1,decreasing=TRUE) # sorts in descending order # Character vector sort v2 <- c("Cherry","BlueBerry","Apple","Pineapple") sort(v2) # sorts in Alphabetical order which is default sort(v2,decreasing=TRUE) # sorts in Reverse Alphabetical order
When we execute the above code, it produces the following result −
[1] 1 2 4 5 7 11
[1] 11 7 5 4 2 1
[1] “Apple” “BlueBerry” “Cherry” “Pineapple”
[1] “Pineapple” “Cherry” “BlueBerry” “Apple”