To get an item in a vector at a specific index in R, you can use indexing with square brackets.
In this example,
myVector
containing elements.[ ]
and specify the index of the item we want to access.# Create a vector
myVector <- c("apple", "banana", "cherry", "date")
# Access item at index 2
accessedItem <- myVector[2]
# Print accessed item
print(accessedItem)
[1] "banana"
In this example,
myVector
containing elements.[ ]
and the vector of indices.# Create a vector
myVector <- c("apple", "banana", "cherry", "date")
# Access multiple items by indices
indices <- c(1, 3)
accessedItems <- myVector[indices]
# Print accessed items
print(accessedItems)
[1] "apple" "cherry"
In this tutorial, we learned How to get Item in a Vector at a Specific Index in R language with well detailed examples.