A vector is one of the basic data structures in R. It contains only similar elements, like strings and numbers, and it can have data types such as logical, double, integer, complex, character, or raw. Let's see how vectors work.
Let's create some vectors by using c():
a<-c(1,3,5,8) a ## [1] 1 3 5 8
On mixing different objects with vector elements, there is a transformation of the elements so that they belong to the same class:
y <- c(1,3) class(y) ## [1] "numeric"
When we apply commands and functions to a vector variable, they are also applied to every element in the vector:
y <- c(1,5,1) y + 3 ## [1] 4 8 4
You can use the : operator if you wish to create a vector of consecutive numbers:
c(1:10) ## [1] 1 2 3 4 5 6 7 8 9 10
Do you need to create more complex vectors? Then use the seq() function. You can create vectors as complex as number of points in an interval or even to find out the step size that we might need in machine learning: