Use Of Vector Datatype In R

Introduction

 
In general, a vector is a one-dimensional sequence, a set of observational data relating to a variable, such as the weight of 5 individuals or the amount of calories you consume every day. Using any of the primitive datatypes, a vector can be generated. The vector should contain components of the same datatype.
 

Create a vector

 
Using the c command, vectors are defined in R. The syntax shows that we have various elements that are going to be combined into a single vector. The syntax to create a vector includes the letter c, with the required elements separated by commas in parentheses.
 
Let's discuss with an example:
  1. ># Creating a vector.  
  2. > person <- c('lucas','sam',"ela",'charlie')  
  3. print(student)  
  4. [1"lucas" "sam"  "ela"  "charlie"   
  5. >   
  6. ># Get the class of our vector.  
  7. print(class(person))  
  8. [1"character"  
  9. >  
With four elements, we created a vector named person. The built-in function class() is determining the datatype or class of the vector, which is the datatype of elements that are enclosed in the vectorfrom the above output, we can conclude that person vector contains elements of character datatype.
 
We also created different vectors containing data of numerical and boolean data types which is discussed below:
  1. > vec_1 <- c(5,4,2.3,7,-8,9# a vector containing numerical data  
  2. > vec_2 <- c(FALSE,TRUE,FALSE,TRUE,FALSE,TRUE#a a vector containing boolean data  
  3. >   
  4. print(vec_1)  
  5. [1]  5.0  4.0  2.3  7.0 -8.0  9.0  
  6. print(vec_2)  
  7. [1]  FALSE  TRUE  FALSE TRUE  FALSE TRUE  
Here, two different vectors of numerical datatype and a boolean datatype have been created.
 
A single vector can comprise of data of more than one previously created vectors.
  1. > v <- c(2,4,5,24)  
  2. > v  
  3. [1]  2  4  5 24  
  4. >   
  5. > abc <- 12.1  
  6. > vec <- c(5,-2,6,7.34,1e+03,64^0.5,2+(3-1.1)/9.44,abc)  
  7. >   
  8. > vec  
  9. [1]    5.000000   -2.000000    6.000000    7.340000 1000.000000    8.000000  
  10. [7]    2.201271   12.100000  
  11. >  
In the above coding snippet, a new vector assigned to the object named vec has been created. The vector contains numerical values and a few of the elements are arithmetic expressions containing arithmetic operators, and the final output of the vector contains numerical data along with the result of arithmetic expression calculated above. The element of the vector abc, which is a numeric object with a value of 32.1 is also included at the end of the vector named vec.
 
Let us have a look at another example:
  1. > vec_3 <- c(v,vec)  
  2. > vec_3  
  3.  [1]    2.000000    4.000000    5.000000   24.000000    5.000000   -2.000000  
  4.  [7]    6.000000    7.340000 1000.000000    8.000000    2.201271   12.100000  
  5. >  
The above syntax created a new vector named vec_3, which contains elements of two previously created vectors named vec and vec joined together to form a single vector.
 

Access Vector Elements

 
To access the location of a particular element in a vector, the index of that element plays a crucial role. The indexing of elements in a vector starts from 1, whereas the index of the last vector element is n, here n signifies the total number of elements of the same datatype contained in that particular vector.
 
For example below i have created two vectors and i also demonstrated the syntax to mention the index of an element:
  1. > a <- c(3,4,6.2,7,-9,1# a vector containing elements of nmeric datatype  
  2. > b <- c(TRUE,FALSE,FALSE,TRUE,FALSE,TRUE) #a vector containing elements of boolean datatype 
  3. >   
  4. print(a[c(1,6)])# accessing 1st and 6th element in a vector  
  5. [1] 3 1  
  6. print(b[c(2,5)]) # access 2nd and 5th element in a vector  
  7. [1]  FALSE FALSE  
  8. >   
Now, I will discuss one of the most common and useful functions related to vectors in R which is the seq() function. I will create a set of elements in a vector which is a sequence of numerical elements in an ascending and descending order. the use of a sequence function in a vector helps in creating applications related to loops and plotting of data points for performing various operations on those data points.
 
The easiest way to create such a sequence, with numeric values separated by intervals of 1, is to use the colon operator. We can use the : operator to create a vector of consecutive numbers. Here are simple examples,
  1. 3:27  
  2.  [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27  
Example 3:27 should be read as “from 3 to 27 (by 1).” The result is a numeric vector just as if you had listed each number manually in parentheses with c.
 
We can also provide either a previously stored value or a (strictly parenthesized) calculation when using the colon operator.
  1. > foo <- 5.3  
  2. > bar <- foo:(-47+1.5)  
  3. >   
  4. > bar  
  5.  [1]   5.3   4.3   3.3   2.3   1.3   0.3  -0.7  -1.7  -2.7  -3.7  -4.7  -5.7  
  6. [13]  -6.7  -7.7  -8.7  -9.7 -10.7 -11.7 -12.7 -13.7 -14.7 -15.7 -16.7 -17.7  
  7. [25] -18.7 -19.7 -20.7 -21.7 -22.7 -23.7 -24.7 -25.7 -26.7 -27.7 -28.7 -29.7  
  8. [37] -30.7 -31.7 -32.7 -33.7 -34.7 -35.7 -36.7 -37.7 -38.7 -39.7 -40.7 -41.7  
  9. [49] -42.7 -43.7 -44.7  
  10. >  
Here is another example:
  1. > v <- 1:8; v  
  2. [11 2 3 4 5 6 7 8  
The above will create a vector with numbers 1 to 8.
 
Here is another example:
  1. v <- 2:-2; v  
The code will return a vector of numbers going backwards, from 2 to -2.
 
Sequences with seq
 
You can also use the seq() function which allows for more flexible creation of sequences. A good example is when you need to define the number of points in an interval, or the step size. Here are some examples:
  1. > seq(13, by=0.2# define the step size  
  2.  [11.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0  
We are printing numbers between 1 and 3 at intervals of 0.2 after each iteration.
 
The seq() function takes in a from value, a to value, and a by value, and it returns the corresponding sequence as a numeric vector.
  1. > seq(from=3,to=27,by=3)  
  2. [1]  3  6  9 12 15 18 21 24 27  
This gives you a sequence with intervals of 3 rather than 1.
 
Instead of providing a by value, we can also specify a length.out value to produce a vector with that many numbers, evenly spaced between the from and to values.
  1. > seq(from=3,to=27,length.out=40)  
  2.  [1]  3.000000  3.615385  4.230769  4.846154  5.461538  6.076923  6.692308  
  3.  [8]  7.307692  7.923077  8.538462  9.153846  9.769231 10.384615 11.000000  
  4. [1511.615385 12.230769 12.846154 13.461538 14.076923 14.692308 15.307692  
  5. [2215.923077 16.538462 17.153846 17.769231 18.384615 19.000000 19.615385  
  6. [2920.230769 20.846154 21.461538 22.076923 22.692308 23.307692 23.923077  
  7. [3624.538462 25.153846 25.769231 26.384615 27.000000  
  8. >  
By setting the length.out to 40, you make the program print exactly 40 evenly spaced numbers from 3 to 27.
 
Here is another example:
  1. > seq(15, length.out=4)   
  2. [11.000000 2.333333 3.666667 5.000000  
For decreasing sequences, the use of by must be negative. Here is an example:
  1. > foo <- 5.3  
  2. > myseq <- seq(from=foo,to=(-47+1.5),by=-2.4)  
  3. > myseq  
  4.  [1]   5.3   2.9   0.5  -1.9  -4.3  -6.7  -9.1 -11.5 -13.9 -16.3 -18.7 -21.1  
  5. [13] -23.5 -25.9 -28.3 -30.7 -33.1 -35.5 -37.9 -40.3 -42.7 -45.1  
  6. >  
This code uses the previously stored object foo as the value for from and uses the parenthesized calculation (-47+1.5) as the to value. Given those values (that is, with foo being greater than (-47+1.5)), the sequence can progress only in negative steps; directly above, we set by to be -2.4.
 
We can also use of length.out to create decreasing sequences. For the same from and to values, you can create a decreasing sequence of length 5 easily, as shown here:
  1. > myseq2 <- seq(from=foo,to=(-47+1.5),length.out=5)  
  2. > myseq2  
  3. [1]   5.3  -7.4 -20.1 -32.8 -45.5  
  4. >  

Modify a vector element

 
It is possible for us to modify a vector, in which we will change the elements contained in it can be achieved by the use of the assignment operator. We can use the techniques for accessing the vector elements so as to modify them. For the purpose of truncating elements, we can use reassignments.
 
Suppose we have the vector x with the following elements. Let's modify the 3rd element of the vector, let us modify the 3rd element of the vector,
  1. > x <- c(1,3,2,4,5)  
  2. > x  
  3. [11 3 2 4 5  
  4. >   
  5. > x[3] <- 0; x  
  6. [11 3 0 4 5  
The 3rd element has been changed from 2 to 0.
 
Let's modify all the vector elements that are less than 4:
  1. > x <- c(1,3,2,4,5)  
  2. > x  
  3. [11 3 2 4 5  
  4.   
  5. > x[x<4] <- 2; x  
  6. [12 2 2 4 5  
  7. >   
All elements below 4 have been changed to 2s.
 
To delete a vector, we simply have to assign to it the value NULL. For example:
  1. > x <- c(1,3,2,4,5)  
  2. > x  
  3. [11 3 2 4 5  
  4.   
  5. > x <- NULL  
  6. > x  
  7. NULL  
  8. >  

Summary

 
In this article, I discussed vectors in R. I demonstrated how to create vectors using the single letter c, with the desired entries in parentheses separated by commas. Accessing and modifying vector elements along with the seq() function to create sequences of vectors were also discussed. Proper coding snippets and output screenshots have been provided as well. 


Similar Articles