For writing an R program to access the last value in a given vector, we are using a built-in function tail(). The tail() function helps to return the last n rows of the dataset. The syntax of this method is like
tail(x,n=number)
where,
In this R program, we are printing the original vectors and then we directly give the values to built-in functions. And print the function result. Here we used variable A for assigning vector values.
STEP 1: Assign variable A with vector values
STEP 2: First we are displaying the original vector values
STEP 3: Call the function tail as tail(A,n=1)
STEP 4: print the result of the function
A= c(32, 40, 12, 23, 27, 38, 9, 17)
print("Original Vectors:")
print(A)
print("Access the last value of the vector:")
print(tail(A, n=1))
[1] "Original Vectors:" [1] 32, 40, 12, 23, 27, 38, 9, 17 [1] "Access the last value of the vector:" [1] 17