w3resource

R Programming: Create vector of numeric, complex, logical and character types of length 6

R Programming: Vector Exercise-1 with Solution

Write a R program to create a vector of a specified type and length. Create vector of numeric, complex, logical and character types of length 6.

Sample Solution :

R Programming Code :

x = vector("numeric", 5)
print("Numeric Type:")
print(x)
c = vector("complex", 5)
print("Complex Type:")
print(c)
l = vector("logical", 5)
print("Logical Type:")
print(l)
chr = vector("character", 5)
print("Character Type:")
print(chr)

Sample Output:

[1] "Numeric Type:"
[1] 0 0 0 0 0
[1] "Complex Type:"
[1] 0+0i 0+0i 0+0i 0+0i 0+0i
[1] "Logical Type:"
[1] FALSE FALSE FALSE FALSE FALSE
[1] "Character Type:"
[1] "" "" "" "" ""                         

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: R Programming Exercises Home.
Next: Write a R program to add two vectors of integers type and length 3.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.