In this article, we will see how to create a vector of specified type and length in R programming language. To create a vector of specified data type and length in R we make use of function vector(). vector() function is also used to create empty vector.
Syntax:
vector(class of the data object, length of the vector)
There is a very straightforward approach for this.
Steps –
- Create vector of required type
- Also pass the size to it
- Here we can also check the type and size of the vector so created
Implementation combined with this approach paints a better picture.
Example 1:
R
a <- vector ( "integer" , 5 )
b <- vector ( "numeric" , 5 )
print (a)
print ( typeof (a))
print ( length (a))
print (b)
print ( typeof (b))
print ( length (b))
|
Output:
[1] 0 0 0 0 0
[1] “integer”
[1] 5
[1] 0 0 0 0 0
[1] “double”
[1] 5
Example 2:
R
a <- vector ( "logical" , 5 )
print (a)
print ( typeof (a))
print ( length (a))
|
Output:
[1] FALSE FALSE FALSE FALSE FALSE
[1] “logical”
[1] 5
Example 3:
R
a <- vector ( "character" , 5 )
print (a)
print ( typeof (a))
print ( length (a))
|
Output:
[1] “” “” “” “” “”
[1] “character”
[1] 5
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Apr, 2021
Like Article
Save Article