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:
- ># Creating a vector.
- > person <- c('lucas','sam',"ela",'charlie')
- > print(student)
- [1] "lucas" "sam" "ela" "charlie"
- >
- ># Get the class of our vector.
- > print(class(person))
- [1] "character"
- >
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:
- > vec_1 <- c(5,4,2.3,7,-8,9) # a vector containing numerical data
- > vec_2 <- c(FALSE,TRUE,FALSE,TRUE,FALSE,TRUE) #a a vector containing boolean data
- >
- > print(vec_1)
- [1] 5.0 4.0 2.3 7.0 -8.0 9.0
- > print(vec_2)
- [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.
- > v <- c(2,4,5,24)
- > v
- [1] 2 4 5 24
- >
- > abc <- 12.1
- > vec <- c(5,-2,6,7.34,1e+03,64^0.5,2+(3-1.1)/9.44,abc)
- >
- > vec
- [1] 5.000000 -2.000000 6.000000 7.340000 1000.000000 8.000000
- [7] 2.201271 12.100000
- >
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:
- > vec_3 <- c(v,vec)
- > vec_3
- [1] 2.000000 4.000000 5.000000 24.000000 5.000000 -2.000000
- [7] 6.000000 7.340000 1000.000000 8.000000 2.201271 12.100000
- >
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:
- > a <- c(3,4,6.2,7,-9,1) # a vector containing elements of nmeric datatype
- > b <- c(TRUE,FALSE,FALSE,TRUE,FALSE,TRUE) #a vector containing elements of boolean datatype
- >
- > print(a[c(1,6)])# accessing 1st and 6th element in a vector
- [1] 3 1
- > print(b[c(2,5)]) # access 2nd and 5th element in a vector
- [1] FALSE FALSE
- >
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,
- > 3:27
- [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.
- > foo <- 5.3
- > bar <- foo:(-47+1.5)
- >
- > bar
- [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
- [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
- [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
- [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
- [49] -42.7 -43.7 -44.7
- >

Join the conversation! Your thoughts help the community grow.