R Programming - Create Vectors using : Operator and seq() Function
R Programming: Vector Exercise-28 with Solution
Write a R program to create a vector using : operator and seq() function.
Sample Solution :
R Programming Code :
# Create a vector 'x' using the : operator, which generates a sequence from 1 to 15
x = 1:15
# Print a message indicating the new vector created with the : operator
print("New vector using : operator-")
# Print the content of the vector 'x'
print(x)
# Print a message indicating the new vector created with the seq() function
print("New vector using seq() function-")
# Print a message specifying that the next vector uses a step size
print("Specify step size:")
# Create a vector 'y' using seq() function from 1 to 3 with a step size of 0.3
y = seq(1, 3, by=0.3)
# Print the content of the vector 'y'
print(y)
# Print a message specifying that the next vector uses a specified length
print("Specify length of the vector:")
# Create a vector 'z' using seq() function from 1 to 5 with a total length of 6 elements
z = seq(1, 5, length.out = 6)
# Print the content of the vector 'z'
print(z)
Output:
[1] "New vector using : operator-" [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [1] "New vector using seq() function-" [1] "Specify step size:" [1] 1.0 1.3 1.6 1.9 2.2 2.5 2.8 [1] "Specify length of the vector:" [1] 1.0 1.8 2.6 3.4 4.2 5.0
Explanation:
- Create a vector using : operator:
- x = 1:15
Creates a vector x containing a sequence of integers from 1 to 15 using the : operator. - Print message (New vector using : operator):
- print("New vector using : operator-")
Prints a message indicating that the following output will show the vector created with the : operator. - Display the vector x:
- print(x)
Displays the content of the vector x. - Print message (New vector using seq() function):
- print("New vector using seq() function-")
Prints a message indicating that the following output will show vectors created using the seq() function. - Print message (Specify step size):
- print("Specify step size:")
Prints a message indicating that the next vector demonstrates a sequence with a specified step size. - Create a vector using seq() with a step size:
- y = seq(1, 3, by=0.3)
Creates a vector y with a sequence from 1 to 3 with a step size of 0.3. - Display the vector y:
- print(y)
Displays the content of the vector y. - Print message (Specify length of the vector):
- print("Specify length of the vector:")
Prints a message indicating that the next vector demonstrates a sequence with a specified length. - Create a vector using seq() with a specified length:
- z = seq(1, 5, length.out = 6)
Creates a vector z with a sequence from 1 to 5, with exactly 6 elements. - Display the vector z:
- print(z)
Displays the content of the vector z.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to add 3 to each element in a given vector. Print the original and new vector.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/r-programming-exercises/vector/r-programming-vector-exercise-28.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics