# Access list elementsmy_list[[1]] # First element (by position)
[1] "John"
my_list[["name"]] # Element by name
[1] "John"
my_list$numbers # Element by name using $ notation
[1] 1 2 3
# Access nested elementsmy_list$numbers[2] # Second element of the numbers vector
[1] 2
my_list$matrix[1,2] # Element at row 1, column 2 of the matrix
[1] 3
Advanced Indexing Techniques
# Using which() for positional indexing from logical conditionsx <-c(5, 10, 15, 20, 25)which(x >15) # Returns positions where condition is TRUE
[1] 4 5
# Using %in% for membership testsfruits <-c("apple", "banana", "cherry", "date")fruits %in%c("banana", "date", "fig") # Tests which elements are in the second vector
[1] FALSE TRUE FALSE TRUE
fruits[fruits %in%c("banana", "date", "fig")] # Select matching elements
[1] "banana" "date"
Remember that R indexing starts at 1, not 0 as in many other programming languages. This is a common source of confusion for beginners coming from languages like Python or JavaScript.