In this tutorial you will learn about the R Vectors and its application with practical example.
R Vectors
Vector is one of the basic data structure in R. Vector is basically a collection of data elements of the same basic type i.e. logical, integer, double, character, complex or raw. The elements in a vector are termed as components.
Creating a Vector in R
Vectors are commonly created using the c() function, it is the easiest way to create vectors in R. While, creating vector we must pass elements of the same type, but, if the elements are of different type then elements are converted to the same data type from lower data type to higher data types from logical to integer to double to character.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
print("W3Adda - R Creating Vectors") # Numerical Vecotr vnum <- c(1, 5, 10) vnum typeof(vnum) # Character Vector vchr <- c("a", "b", "c") vchr typeof(vchr) # Boolean Vector vbool <- c(TRUE, FALSE, TRUE) vbool typeof(vbool) x <- c(5, 3.2, TRUE, "w3adda") # Converted to Characters x typeof(x) |
Output:-
Vectors of consecutive or sequential numeric values can simply be generated using colon (:) operator as following –
Syntax:-
1 2 3 |
c(start:end) or x <- start:end |
start:- It is the start/first element value.
end:- It is the end/last element value.
Example:-
1 2 3 4 5 |
print("W3Adda - R Creating Vector using Colon") x <- 1:5 x y <- 3:-3 y |
Output:-
Vector using seq() function
The seq() function enable us to create vectors with sequential values at specified step size.
Syntax:-
1 |
seq(startValue, endValue, by=stepSize) |
Above is the general syntax of seq() function, here
stepSize:- It is step or interval size between vector elements.
startValue:- It is the start value.
endValue:- It is the end value.
Example:-
1 2 3 4 5 6 7 |
print("W3Adda - R Creating Vecotr using seq function") a <- seq(1,5,by=1) a b <- seq(1,5,by=2) b c <- seq(1,5,by=3) c |
Output:-
Accessing Vector Elements
Vector elements can be accessed by passing index value(s) in brackets [ ]. An index value can be logical, integer or character.
integer Index:-
An integer index can be used to denote the element position. An integer index value start with 1.
logical Index:-
TRUE, FALSE or 0 and 1 can be used as logical index corresponding to the vector element position, vector with TRUE index position is returned.
character Index:-
Character index vector can be used with named vectors.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Accessing vector elements using integer indexing. t <- c("John","Smith","Steve","Json","Alex","Murphy","Sophie") u <- t[c(2,4,6)] print(u) # Accessing vector elements using logical indexing. v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)] print(v) # Accessing vector elements using negative indexing. x <- t[c(-2,-4)] print(x) # Accessing vector elements using 0/1 indexing. y <- t[c(0,0,0,0,0,0,1)] print(y) # Accessing vector elements using character indexing. a <- c("sun" = "Sunday","mon" = "Monday","tue" = "Tuesday","wec" = "Wednesday","thu" = "Thursday","fri" = "Friday","sat" = "Saturday") b <- a[c("sun","sat")] print(b) |
Output:-
Modifying Vector Elements
Vector element value can be modified by assigning it new value as following –
Example:-
1 2 3 4 5 6 7 |
print("W3Adda - R Modify Vector Element") x <- c(0, 2, 5, 6, 8, 10) print("W3Adda - R Vector Before Update") print(x) x[3] <- 4 print("W3Adda - R Vector After Update") print(x) |
Output:-
Deleting Vector
Vector can be deleted by simply assigning it NULL value as following –
Example:-
1 2 3 4 5 6 7 |
print("W3Adda - R Delete Vector") x <- c(0, 2, 5, 6, 8, 10) print("W3Adda - R Vector Before Delete") print(x) x <- NULL print("W3Adda - R Vector After Delete") print(x) |
Output:-