Learn R Online
 Home            

R Vector

In this tutorial you will learn about the concept of vector in R Programming. Learn how to create, access, modify, sort and delete a vector.

A vector is the simplest type of data structure in R. It contains items of same type. The items or members of a vector are called its components.

Vector Types:

There are six types of atomic vectors 

1. integer - this type contains only positive and negative integers

2. double - this type contains floating point numbers

3. character - consisting of character or character string

4. logical - consisting of boolean values TRUE or FALSE

5. complex - consisting of complex numbers

6. raw - raw data in binary form

A vector may contain one value or more than one. 

typeof(x) function is used to check the type of a given vector.

Total elements in a vector or the size of vector is obtained by the function length(x). To see if an object is a vector is.vector(x) function is used.

How to create vector in R

If you want to create a vector with only one value or in simple words it has one component and has a length equal to one, simply write the name of vector and assign it the single value. 

a <- 2.5

b <- 5L

c <- "A"

Here in first example name of vector is a and 2.5 is assigned to it. <- symbol is called assignment operator it assigns the value at its right side to the variable on its left. 2.5 is a floating point number hence a is atomic vector of type double. In second example b is atomic vector of type integer. By writing L after a whole number creates an integer atomic type. c is of character type. See here the assignments, with typeof, length and is.vector functions in RStudio.

Vector in R

Usually a vector consists of more than one component. To create it we use c() function in R, where c stands for concatenate or simply combine. It is clear that c() is not creating a vector but it is combining different values into one vector. Lets suppose you want to create a vector with five members 1,3,5,7,9 its syntax in R is 

How to Create a Vector in R

After creating v, use is.vector(v), typeof(v) and length(v) to check about characteristics of v.

As discussed earlier vector contains member of the same data type. If you combine items of different atomic data types then coercion takes place. Coercion means out of two or more data types R programming will make elements of the same data type. The direction of coercion is      

logical > integer > double > character

If you combine logical and integer data type, the data type of vector will be integer and so on.

> x <- c(TRUE,3L,FALSE,5L)
> is.vector(x)
[1] TRUE
> typeof(x)
[1] "integer"

And if you combine all data types the resultant vector will be of which data type? Yes, character data type as it is the highest in coercion.

Vector Creation with : operator

To create a numeric vector consisting of some simple sequence we use : colon operator. 

If you want to create a vector of numbers from 1 to 7 

> v <- c(1:7)
> v
[1] 1 2 3 4 5 6 7
> typeof(v)
[1] "integer"

Creating vector in reverse order

> v <- c(-3:3) ; v
[1] -3 -2 -1 0 1 2 3

Vector Creation with seq() function

seq() function is used to create complex sequence. 

The syntax of this function is 

seq(start, stop, step)

where start is the begining and stop is the end of sequence while step is the increment

> seq(3,5,0.5)
[1] 3.0 3.5 4.0 4.5 5.0

Now using this function to create a vector

> v <- c(seq(3,5,0.5)); v
[1] 3.0 3.5 4.0 4.5 5.0

Another form of same function, uses argument length.out to determine the size or length of the vector

seq(start, stop, length.out=size)

> v <- c(seq(1,9,length.out=7)); v
[1] 1.000000 2.333333 3.666667 5.000000 6.333333 7.666667  9.000000

In this example from 1 to 9, total 7 values are generated which are equidistant from each other. So if you want to create regular points after specific intervals in between two points you may use seq() function with length.out argument. 

Accessing Vector Elements

Vector elements are accessed by index. In R programming index starts from 1. The syntax is 

vector [index]

Integer as index

where vector is name of vector followed by index number with in square brackets [].

> v <- c(seq(4,12,2)); v
[1] 4 6 8 10 12
> v[3]
[1] 8
> v[5]
[1] 12

We have accessed the third and fifth element of vector v by using 3 and 5 as the index respectively.

In case you want to access all elements leaving only the 2 component use -2 as index, it will show all elements leaving the second element.

> v[-2]
[1] 4 8 10 12

for accessing a sequence with in a vector use start and stop as index

> v[2:4]
[1] 6 8 10

if you want to access some specific elements, use c() function and quote index number. Like if you want to access second and fourth element use

> v[c(2,4)]
[1] 6 10

Logical vector as index

If you want to create a new vector sliced from a given vector based on logical vector as index create a new logical vector which has the same length as of the original vector, set the values as TRUE for the elements to be included and FALSE for values to be excluded. 

For example the original vector v is 

> v <- c ("Hello", "there","to","how","R Programming")

We want a vector which contains the first, third and fifth element.

Now the first step is to create a  logical vector with 1,3,5 elements as TRUE as we wish to include these elements and remaining as FALSE

> lv <- c(TRUE, FALSE, TRUE, FALSE, TRUE)

In second step we provide this logical vector as index

> v[lv]

The output in RStudio is

> v <- c ("Hello", "there","to","how","R Programming")
> lv <- c(TRUE, FALSE, TRUE, FALSE, TRUE)
> v[lv]
[1] "Hello" "to" "R Programming"

Character vector as index

We can also assign names to vector elements with the help of characters and then access these elements with the names as index.

> numbers <- c(1,2,3,4,5)
> names(numbers) <- c("first","second","middle","forth","last")
> numbers
first second middle forth last
1         2        3       4     5
> numbers["first"]
first
1
> numbers[c("middle","last")]
middle last
    3      5

Vector arithmetics

Vectors are unique feature of R programming which make it very functional and efficient for statistics and data science. 

If you want to multiply each element of a vector by a number, instead of multiplying each element one by one with a loop simply multiply whole vector with that number.

> a <- c(1,2,3,4,5)
> a * 3
[1] 3 6 9 12 15

Similarly you can do addition, subtraction, division operations on a vector as a whole without a for loop or while loop. See another example in which we apply modulus operator on vector a. Modulus operator %% returns the remainder of integer division.

> a %% 2
[1] 1 0 1 0 1

> a ^ 2    # squaring each element of a
[1] 1 4 9 16 25

For adding two vectors, they must be of same length. The corresponding elements will be added like first element of first vector and first element of second vector will be added.

> x <- c(1,3,5)
> y <- c(2,4,6)
> x + y
[1] 3 7 11

Similarly for subtraction the length of vectors should be same.

> y - x
[1] 1 1 1

Vector Recycling

If two vectors are not of same length in an operation then the shorter vector will be recycled to match size of longer vector. Here first vector has 2 elements and second vector has 6 elements. If we add both these vectors then the first vector will repeat itself two times to match second vector and then addition will take place. 

> first <- c(1,2)
> second <- c(10,20,30,40,50,60)
> first + second
[1] 11 22 31 42 51 62

Here first becomes (1,2,1,2,1,2) and then added in second vector.

How to Modify a Vector in R

We can modify a vector with the help of index and assignment operator but first we access the specific element which is to be modified. We can also decrease or increase the length of a vector. 

First we declare a vector with 5 items

> j <- c(1,3,6,9,12)

Now if we want to change value of last element first we will access it by index and then use assignment operator to assign it a new value, later we check it to see if vector is modified.

> j[5] <- 100
> j
[1] 1 3 6 9 100

If there is a condition that the smallest value in your data should be 5. Now we want to assign any value which is less than 5 as 5

> j[j<5] <- 5
> j
[1] 5 5 6 9 100

We can add a new vector in given vector with c() function or use append() function in case we want to add it at a specific location

> k <- c(5,10,15)
> j <- c(j,k)
> j
[1] 5 5 6 9 100 5 10 15

To truncate a vector, assign only the items you want to keep. If we want to remove k from j

> j <- j[1:5]
> j
[1] 5 5 6 9 100

Delete a Vector in R

If you do not need a vector, you may want to delete it. It is done by simply assigning NULL to it.

> j <- NULL
> j
NULL
> j[3]
NULL

This will release any resources held by vector j. It is a good practice to delete an object if you don't need it any more. 

Sorting a Vector

Elements of a vector can be sorted with sort() function. For vectors with numeric data you can apply increasing or decreasing order. 

> x <- c(3,1,99,33,66,3)
> sort(x)
[1] 1 3 3 33 66 99
> sort(x,decreasing=TRUE)
[1] 99 66 33 3 3 1

The default argument for sort function is increasing = TRUE which means only providing one argument, the vector to sort will result in sorting in increasing order. However if you want to sort it from largest value to smallest, you have to provide decreasing = TRUE as argument. 

For character type vector the resultant vector will be sorted alphabetically

> fruits <- c("Apple","Orange","Guava","Mango","Grape")
> sort(fruits)
[1] "Apple" "Grape" "Guava" "Mango" "Orange"
> sort(fruits, decreasing=TRUE)
[1] "Orange" "Mango" "Guava" "Grape" "Apple"