The Vector is the most basic Data structure in R programming. In R Vector can hold a collection of similar type of elements (type may be integer, double, char, Boolean etc) and if you type different data types in single vector then, all the elements will be converted to single type. In this article we will show you, How to Create a Vector, How to Access and manipulate the Vector Elements, Performing Arithmetic Operations on vectors with example,

## Creating a Vector

In this example, we will create a vector of single element. Following code snippet will show you the most basic way to create a Vector.

**TIP:** Every variable will be internally converted to Vector

**R CODE**

1 2 3 4 5 |
# Vector with 1 integer Element print(10L) # Vector with 1 String element print("Hello") |

**OUTPUT**

### Creating R Vector using Range

In R programming there is a special operator called Range or Colon and this will help to create a vector. For example, i <- 1:10 means 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Vector with Range i <- 1:10 print(i) # Vector with Decimal Range j <- 1.5:5.5 print(j) # Vector with Decimal Range k <- 1.5:5.5 print(k) # Vector with Decimal Range n <- -10:-20 print(n) # Letter Vector with Range l <- LETTERS[1:6] print(l) |

**OUTPUT**

### Creating R Vector using Sequence (seq) Operator

In this example we will show you, How to create a vector using sequence operator, or simply seq operator. The Sequence operator will return values sequentially.

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Vector with Sequence a <- seq(from = 1, to = 10, by = 1) print(a) # Here, from =, to =, by = values are option so you can remove them too b <- seq(11, to = 15, by = 1) # Removing from print(b) c <- seq(15, 25, by = 3) # Removing both from, to print(c) d <- seq(2, 20, 2) # removing from, to, and by print(d) |

**ANALYSIS**

Below statement will create a Vector starting with Value 1 at index position 1 and ends with Value 10 at index position 10 by increment the value by 1. It means 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

1 |
a <- seq(from = 1, to = 10, by = 1) |

**OUTPUT**

### Creating R Vector using concatenation c

In this example we will show you, How to create a vector using c, or we say concatenation. This is the most popular approach and normally we prefer this way

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# Vector with Concatenation # Numeric Vector a <- c(1, 2, 3, 4) print(a) # Character vector b <- c("India", "China", "Japan", "Russia", "Sri Lanka") print(b) #Boolean vector d <- c(TRUE, FALSE, FALSE, TRUE, TRUE) print(d) # Mixed Vector and its Type will be Character e <- c("India", 2, "China", 1, TRUE) print(e) typeof(e) # this will return the Data type of Vector <em><strong>e</strong></em> # Placing or Nesting One Vector inside the another f <- c("UK", "USA", TRUE, FALSE, b) # b is another Vector print(f) |

**OUTPUT**

## Accessing R Vector Elements

In R programming, We can use the index position to access the elements in a Vector. Using this index value, we can access or alter/change each and every individual element present in the vector. Index value starts at 1 and end at n where n is the vector length. For example, if we declare a vector that stores 10 elements then index starts at 1 and ends at 10. To access or alter 1^{st} value use Vector_Name[1] and to alter or access the 10^{th} value, use Vector_Name[10]. Lets see the example for better understanding:

**R CODE**

1 2 3 4 5 6 7 8 9 |
# R Vector Elements Accessing a <- c("India", "China", "Japan", "UK", "USA", "Russia", "Sri Lanka") print(a) print(a[1]) print(a[3]) print(a[1:3]) print(a[4:6]) |

**OUTPUT**

**ANALYSIS**

First we declared a Vector called * a* and assigned the following values

1 |
a <- c("India", "China", "Japan", "UK", "USA", "Russia", "Sri Lanka") |

From the below statements a[1] means element at first position (i.e., India), and a[3] means element at third position (i.e., Japan)

1 2 |
print(a[1]) print(a[3]) |

In the next line, we used the special operator Range (or Colon). Below statement will print the elements from 1 through 3 (i.e., 1, 2, 3)

1 |
print(a[1:3]) |

### Access using Vector

In this example we will show you, How to access the Vector elements using another Vector.

**R CODE**

1 2 3 4 5 6 7 8 9 10 |
# R Vector Elements Accessing a <- c("India", "China", "Japan", "UK", "USA", "Russia", "Sri Lanka") print(a) b <- c(2, 4, 6) print(a[b]) print(a[c(5, 7)]) print(a[c(7, 4, 1)]) |

**ANALYSIS**

First we declared a vector of numbers 2, 4, 6

1 |
b <- c(2, 4, 6) |

In the next line we used those numbers as the index position to access the Vector * a* elements. It means, print the elements at index position 2, 4 and 6

1 |
print(a[b]) |

**OUTPUT**

### Using Negative Values

In this example we will show you, How to access the Vector elements using Negative values and the Boolean values. In R Vectors, Negative index position are used to omit those values

**R CODE**

1 2 3 4 5 6 7 8 9 10 |
# R Vector Elements Accessing a <- c("India", "China", "Japan", "UK", "USA", "Russia", "Sri Lanka") print(a) b <- c(-3, -7) print(a[b]) print(a[c(-4, -6, -7)]) print(a[c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE)]) |

**OUTPUT**

**ANALYSIS**

First we declared a vector of negative numbers -3 and -7

1 |
b <- c(-3, -7) |

In the next line we used those numbers as the index position to access the Vector * a* elements. It means, print the Vector elements except the values at index position 3 and 7

1 |
print(a[b]) |

In the next line we declared a Boolean vector and used those Boolean values as the index position to access the Vector * a* elements. Here, TRUE means print the value, and FALSE means don’t print. It means, print elements at position 1, 3, 4, 6

1 |
print(a[c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE)]) |

### Use Character Vectors as Index

In this example we will show you, How to access the Vector elements using Character Vectors Index values. From the below code snippet you can observe that, we declare a vector with alphabet indexes. This can help us to extract the vector elements using the alphabets.

**R CODE**

1 2 3 4 5 6 7 8 9 |
# R Vector Elements Accessing v <- c("a" = 10, "b" = 20, "c" = 30, "d" = -15, "e" = 40) print(v) print(v["a"]) print(v["d"]) print(v[c("a", "c")]) |

**OUTPUT**

## Manipulating R Vector Elements

In R Programming, we can manipulate the Vector elements in following ways:

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# R Vector Elements Manipulation a <- c(10, 20, 30, -15, 40, -25, 60, -5) print(a) a[7] <- 77 print(a) a[a < 0] <- 99 print(a) # Truncating the Vector Elements a <- a[1:5] print(a) # Deleting Vector a <- NULL print(a) |

**OUTPUT**

**ANALYSIS**

Below statement will assign 77 to vector element at position 7.

1 |
a[7] <- 77 |

Following statement will assign 99 to all the elements whose values are less than 0. Here * a < 0* will check whether the element is less than zero or not and if the condition is true then that element will be replaced by 99

1 |
a[a < 0] <- 99 |

In R Slice, the first integer value is the index position where the slicing will start and the second integer value is the index position where the slicing will end.

1 |
a <- a[1:5] |

Following statement will remove the vector completely

1 |
a <- NULL |

## Important Functions

Following functions are some of the most useful functions supported by the Vectors in R programming.

**typeof(Vector):**This method will tell you the data type of the vector**Sort(Vector):**This method will help us to sort the items in Ascending order**length(Vector):**This method will count number of elements in a vector**head(Vector, limit):**This method will return top six elements (if you Omit the limit). If you specify the limit as 4 then, it will return first 4 elements.**tail(Vector, limit):**This method will return last six elements (if you Omit the limit). If you specify the limit as 2 then, it will return last two elements.

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# R Vector Elements Accessing a <- c(10, 20, 30, -15, 40, -25, 60, -5) print(a) # Some of the Important Functions typeof(a) length(a) head(a) head(a, 4) tail(a) tail(a, 3) x <- sort(a) print(x) |

**OUTPUT**

## Arithmetic Operations on R Vector

In this example we will show you, How to use R Arithmetic Operators on Vectors to perform arithmetic Operations.

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 |
a <- c(10, 20, 30, 40) b <- c(12, 26, 38, 44) print(a + b) print(b - a) print(a * b) print (2 * a) d <- c(12, 44) print(a + d) |

**OUTPUT**

**ANALYSIS**

First we declared two numeric vectors of same length

1 2 |
a <- c(10, 20, 30, 40) b <- c(12, 26, 38, 44) |

Next, we performed addition, subtraction and multiplication on them. Here * a + b *means (10 + 12, 20 + 26, 30 + 38, 40 + 44) = (22, 46, 38, 44)

1 2 3 |
print(a + b) print(b - a) print(a * b) |

Above things will work as long as the vector length are same but, if the lengths are different then vector element recycling will happen. It will travel and multiply each element with 2 until it finish the vector elements.

1 |
print (2 * a) |

Next, we declared a vector * d* of two elements and performed the addition. Here

*means (10 + 12, 20 + 44, 30 + 12, 40 + 44) because we have only two elements in*

**a + d***so those two elements will be repeated.*

**d**
1 2 |
d <- c(12, 44) print(a + d) |

Thank You for Visiting Our Blog

## Leave a Reply