To get the first element in an R list, you can use indexing with the index 1.
In this example,
numbers
with integer elements.[1]
.numbers <- c(10, 20, 30, 40, 50)
first_element <- numbers[1]
print(paste("First element is:", first_element))
[1] First element is: 10
In this example,
names
with string elements.[1]
.names <- c("Alice", "Bob", "Charlie", "David")
first_element <- names[1]
print(paste("First element is:", first_element))
[1] First element is: Alice
In this tutorial, we learned How to get First Element in a List in R language with well detailed examples.