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
Here x is an R object, for example, a matrix, array, or data frame.
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.
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
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