Vectors can be indexed using numeric positions, negative indices (to exclude), or logical vectors.
Use [[]] or $ for direct access to list elements.
Use [row, column] format to extract values or slices from matrices.
Data frames can be subset by position or logical condition.
Use logical comparisons to filter values dynamically.
nums <- c(5, 10, 15, 20)
nums[nums > 10] # 15, 20
nums[nums %% 2 == 0] # even numbersPrevent R from simplifying output (e.g., to vector) by setting drop=FALSE.
m[1, , drop=FALSE] # Keeps first row as a matrix
df[ , "age", drop=FALSE] # Keeps column as data framestr() or class()which() to get positions of TRUE valuesAsk the AI if you need help understanding or want to dive deeper in any topic