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

How to create a vector and find the length and the dimension of the vector

Here we are explaining how to write an R program to create a vector and find the length and the dimension of the vector. Here we are using a built-in function dim(),length() for this finding. The function dim() helps to retrieve or set the dimension of an object. And the function 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 functions are

dim(x); length(x) – Where x is a R object

Here x is an R object, for example, a matrix, array, or data frame.

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

Below are the steps used in the R program to create a vector and find the length and dimension of the vector in the R program. In this R program, we directly give the values to built-in functions. For that first, 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: First print original 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:")
## [1] "Original vectors is:"
print(A)
## [1] 1 3 5 7 9 6
print("Dimension of the vector is:")
## [1] "Dimension of the vector is:"
print(dim(A))
## NULL
print("length of the vector is:")
## [1] "length of the vector is:"
print(length(A))
## [1] 6

R Source Code

A = c(1,3,5,7,9,6,11,31,15,71,19,61)

print("Original vectors is:")
## [1] "Original vectors is:"
print(A)
##  [1]  1  3  5  7  9  6 11 31 15 71 19 61
print("Dimension of the vector is:")
## [1] "Dimension of the vector is:"
print(dim(A))
## NULL
print("length of the vector is:")
## [1] "length of the vector is:"
print(length(A))
## [1] 12