R Program to access the first value in a given vector

How to access the first value in a given vector

Here we are explaining how to write an R program to access the first value in a given vector. Here we are using a built-in function head() for this finding. The head() function helps to returns the first n rows of the dataset. The syntax of this method is like

head(x, n = number)
Where x is the input dataset/dataframe and n is the number of rows that the function should display.

How to access the first value in a given vector in R program

Below are the steps used in the R program to get the first 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.

ALGORITHM

  • STEP 1: Assign variable A with vector values
  • STEP 2: First print original vector values
  • STEP 3: Call the function tail as head(A, n=1)
  • STEP 4: print the result of the function

R Source Code

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 first value of the vector:")
## [1] "Access the first value of the vector:"
print(head(A, n=1))
## [1] 32

R Source Code

B = c(321, 420, 112, 523, 227, 238, 29, 817)

print("Original Vectors:")
## [1] "Original Vectors:"
print(B)
## [1] 321 420 112 523 227 238  29 817
print("Access the first value of the vector:")
## [1] "Access the first value of the vector:"
print(head(B, n=1))
## [1] 321