To access elements in a list in R, you can use indexing or iterate over the list using loops.
In this example,
num_vector
with numeric elements.num_vector <- c(1, 2, 3, 4, 5)
element <- num_vector[3] # Accessing element at index 3
print(paste("Element at index 3:", element))
[1] "Element at index 3: 3"
In this example,
str_vector
with string elements.str_vector <- c("Apple", "Banana", "Cherry")
for (item in str_vector) {
print(item)
}
[1] "Apple" [1] "Banana" [1] "Cherry"
In this tutorial, we learned How to Access Elements in a List in R language with well detailed examples.