Here we are explaining how to write an R program to access the last value in a given vector. Here we are using a built-in function tail() for this finding. The tail() function helps to returns the last n rows of the dataset.The syntax of this method is like
Below are the steps used in the R program to get the last value in a given vector. In this R program, we directly give the values to built-in functions. And print the function result. Here we used variable A for assigning vector values.
A = c(32, 40, 12, 23, 27, 38, 9, 17)
print("Original Vectors:")
## [1] "Original Vectors:"
print(A)
## [1] 32 40 12 23 27 38 9 17
print("Access the last value of the vector:")
## [1] "Access the last value of the vector:"
print(tail(A, n = 1))
## [1] 17
B = c(14, 78, 25, 45, 95, 34, 52, 61)
print("Original Vectors:")
## [1] "Original Vectors:"
print(B)
## [1] 14 78 25 45 95 34 52 61
print("Access the last value of the vector:")
## [1] "Access the last value of the vector:"
print(tail(B, n = 1))
## [1] 61