R Program to create a vector and find the length and the dimension of the vector


February 25, 2023, Learn eTutorial
1056

How to create a vector and find the length and the dimension of the vector using R program?

To write an R program for creating a vector and calculate its length and its dimension, we are using the built-in functions dim(), and length().

  • dim() helps retrieve or set an object's dimension.
  • length() is used to get or set the length of vectors (including lists) and factors, and of any other R object.

The syntax of these are 

dim(x)
length(x) 
 
  • x is an R object, for example, a matrix, array, or data frame.

In this R program, we directly give the values to built-in functions, before that we are displaying the vector values. Then, we consider the variable A in which vector value is assigned. Call the functions dim() for finding dimension and length() for finding the length of the vector. Finally, print the result.

ALGORITHM

STEP 1: Assign variable A with vector values

STEP 2: Display the real vector values

STEP 3: Call the function dim() as dim(A)

STEP 4: print the result of the function

STEP 5: Call the function length() as length(A)

STEP 6: print the result of the function

 

R Source Code

                                          A = c(1,3,5,7,9,6)
print("Original vectors is:")
print(A)
print("Dimension of the vector is:")
print(dim(A))
print("length of the vector is:")
print(length(A))
                                      

OUTPUT

[1] "Original vectors is:"
[1] 1 3 5 7 9 6
[1] "Dimension of the vector is:"
NULL
[1] "length of the vector is:"
[1] 6