In this tutorial you will learn about the concept of vectors 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. 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.
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.
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
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 > characterIf you combine logical and integer data type, the data type of vector will be integer and so on.
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.
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
<- c(-3:3) ; v
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
Now using this function to create a vector
<- c(seq(3,5,0.5)); v
Another form of same function, uses argument length.out to determine the size or length of the vector
seq(start, stop, length.out=size)
<- c(seq(1,9,length.out=7)); v
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.
Vector elements are accessed by index. In R programming index starts from 1. The syntax is
where vector is name of vector followed by index number with in square brackets .
<- c(seq(4,12,2)); v
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.
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
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
The output in RStudio is
<- c ("Hello", "there","to","how","R Programming")
> lv <- c(TRUE, FALSE, TRUE, FALSE, TRUE)
We can also assign names to vector elements with the help of characters and then access these elements with the names as index.
> names(numbers) <- c("first","second","middle","forth","last")
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 * 3
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.
^ 2 # squaring each element of a
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.
> y <- c(2,4,6)
> x + y
Similarly for subtraction the length of vectors should be same.
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.
> second <- c(10,20,30,40,50,60)
> first + second
Here first becomes (1,2,1,2,1,2) and then added in second vector.
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.
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
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
> j <- c(j,k)
To truncate a vector, assign only the items you want to keep. If we want to remove k from j
If you do not need a vector, you may want to delete it. It is done by simply assigning NULL to it.
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.
Elements of a vector can be sorted with sort() function. For vectors with numeric data you can apply increasing or decreasing order.
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
> sort(fruits, decreasing=TRUE)