 Recent Tutorials and Articles
Vectors in R Language
Published on: 25th May 2018

This article provides you with a good foundation on vectors in R language by covering their basics, arithmetic operations, numeric and name indexes based operations.

Pre-requisites

You will need R language installed on your PC before you can follow below tutorial. In case, you haven't installed R language and environment already, you can download it from here for your operating system and simply run the installer.

What are Vectors in R?

Just like any other language, A vector in R language is a container for data elements of the same basic type in sequence. In some of other languages, these are also referred as one dimenstional array. Data elements in a vector are called components.

Here is how you would create a vector containing three numberic values 1, 2, 3 and 4 using R Gui console -

``````> c(1, 2, 3, 4)
 1 2 3 4
```
```

And here is how you can get the length of a vector -

``````> length(c(1, 2, 3, 4))
 4
```
```
Vector Operations

R language provides the functions for performing various operations on vectors. We will be starting with combining two vectors using function called C as shown below -

``````> first = c(1, 2, 3, 4)
> second = c(5, 6, 7, 8)

> c(first, second)
 1 2 3 4 5 6 7 8
```
```

Let's see how the combining of vectors happen when two vectors are of different types -

``````> first = c(1, 2, 3, 4)
> second = c("a", "b", "c", "d")

> c(first, second)
 "1" "2" "3" "4" "a" "b" "c" "d"
```
```

As shown above, the type of elements of first vector is changed to character while combining it with second vector. This process is called Value Coercion.

Vector Arithmetic Operations

After combining of two vectors, it's time to talk about the arithmetic operations supported by vectors in R. For demonstrating these operations, we will be utilizing the following vectors -

``````> first = c(1, 2, 3, 4)
> second = c(5, 6, 7, 8)
```
```

Here is how you can do perform various arithmetic operations such as addition, subtraction, multiplication and divison with a scalar value -

``````> first
 1 2 3 4

> first + 2
 3 4 5 6

> first - 2
 -1  0  1  2

> first * 2
 2 4 6 8

> first / 2
 0.5 1.0 1.5 2.0
```
```

Similarly, we can perform arithmetic operations between two vectors via components-wise operations -

``````> first
 1 2 3 4

> second
 5 6 7 8

> first + second
  6  8 10 12

> first - second
 -4 -4 -4 -4

> first * second
  5 12 21 32

> first / second
 0.2000000 0.3333333 0.4285714 0.5000000
```
```

You might wonder that what would happen if these vectors had different lengths? Well, in this case, R will expand the vector with lesser length to make it equal with other vector by repeating the elements. This process of expanding the length of vector is called Vector Recycling.

To demonstrate vector recycling, let's redefine the first variable by assigning it a vector of length 8 and perform the addition operation again as shown below -

``````> first = c(1, 2, 3, 4, 5, 6, 7, 8)

> first
 1 2 3 4 5 6 7 8

> second
 5 6 7 8

> first + second
  6  8 10 12 10 12 14 16
```
```

As demonstrated in above code, R converts second vector into (5, 6, 7, 8, 5, 6, 7, 8) to make it equal to second vector. However, please note that there are no changes to original second vector as this expansion is done in memory just to perform arithmetic operations. Hence, if we printed second vector, it will still show 5 6 7 8 as shown below -

``````> second
 5 6 7 8
```
```
Vector Index Operations

Let's look at the vector index related operations such as getting, adding and removing the components by their indexes. Unlike some languages, components index of vectors in R starts with one. We will be using following vector for demonstrating index operations -

``````> popularLanguages = c("R","C", "Java", "PHP", "Python")
> popularLanguages
 "R"      "C"      "Java"   "PHP"    "Python"
```
```

Following code snippet shows how to perform basic index based vector operations -

``````> popularLanguages
 "R"      "C"      "Java"   "PHP"    "Python"

> popularLanguages #getting second component
 "C"

> popularLanguages[-2] #deleting second component
 "R"      "Java"   "PHP"    "Python"

> popularLanguages = "C" #adding back second component

> popularLanguages
 "R"      "C"      "Java"   "PHP"    "Python"
```
```

After basic operations, let's see how to slice a new vector from an existing vector. To get this done, instead of passing single index, we either pass vector of indexes or range of vectors as shown below:

``````> popularLanguages
 "R"      "C"      "Java"   "PHP"    "Python"

> popularLanguages[c(3, 4, 5)] # create a new vector with the 3rd, 4th and 5th components of popularLanguages vector
 "Java"   "PHP"    "Python"

> popularLanguages[c(3, 4, 5, 5)] # same index can be used multiple times to duplicate the components in the new vector
 "Java"   "PHP"    "Python" "Python"

> popularLanguages[c(4, 3, 4, 5)] # indexes can be in any order
 "PHP"    "Java"   "PHP"    "Python"

> popularLanguages[3:5] # colon(:) operation can be used for specifying the range of components to pick
 "Java"   "PHP"    "Python"

> popularLanguages[5:3] # range can be in reverse order as well
 "Python" "PHP"    "Java"
```
```

Before we conclue the vectors in R, there is one more functionality worth convering for assigning the names to components of a vector. Please refer following code snippet to see how you can assign names to your components in order to retrieve the vector components later by their names -

``````> name = c("R", "Statistical Language")

> name
 "R"                    "Statistical Language"

> names(name) = c("Language Name", "Language Type") # Use names function to name the components

> name
Language Name          Language Type
"R"   "Statistical Language"
```
```

Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

Published on: 25th May 2018