To get the first item in a vector in R, you can use the indexing notation with the number 1 inside square brackets ([]).
In this example,
intVector
containing integer values.[1]
to access the first item in the integer vector.intVector <- c(10, 20, 30, 40)
firstIntItem <- intVector[1]
print(firstIntItem)
[1] 10
In this example,
charVector
containing character values.[1]
to access the first item in the character vector.charVector <- c('apple', 'banana', 'cherry')
firstCharItem <- charVector[1]
print(firstCharItem)
[1] "apple"
In this tutorial, we learned How to get First Item in a Vector in R language with well detailed examples.