You can create a Vector in R using c()
primitive function. In R, the Vector contains elements of the same type and the types can be logical, integer, double, character, complex or raw. You can create a Vector using c()
. I have covered more Vector and Data Frame examples in R Programming Tutorial.
1. Create an R Vector
In R, Vector is a basic data structure that is used to store elements of the same data type. and the types can be logical, integer, double, character, complex or raw. R Vector is created by using c(). Let’s see syntax and create a vector.
2. Syntax of c()
Following is a syntax of c() function that is used to create a Vector in R.
# Syntax of c() function
c(...)
3. Create a Vector Example
Now let’s create a vector using c() primitive function.
# Create Vectors
id <- c(10,11,12,13)
name <- c('sai','ram','deepika','sahithi')
dob <- as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16'))
Here variables
id
– Numeric Vector which stores the numeric values.name
– Character Vector which stores the character values.dob
– Date Vector which stores the date values.
The above example creates 3 Vectors, now let’s display the type of these Vector variables.
# Types of Vectors
> typeof(id)
#[1] "double"
> typeof(name)
#[1] "character"
> typeof(dob)
#[1] "double"
4. Conclusion
In this article, you have learned what is a Vector and how to create it by using c() primitive function and also learned the data types of these created Vectors.