R Program to access the first value in a given vector


February 8, 2023, Learn eTutorial
1625

How to access the first value in a given vector using R programming?

Here we explain how to write an R program to access the first value in a given vector. For that, we are using a built-in function head(). The head() function helps to return the first n rows of the dataset. The syntax of the head() function is like 

head(x,n=number)

where,

  • x is the input dataset/data frame
  • n is the number of rows that the function should display.

In this R program, we are printing the original vector values, then 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: Display the original values of vector

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

OUTPUT

[1] "Original Vectors:"
[1] 32, 40, 12, 23, 27, 38, 9, 17
[1] "Access the first value of the vector:"
[1] 32