Summary
You can now select and replace parts of an object by position, name, or condition, while checking the structure of the result.
What you learned
| Task | Syntax | Remember |
|---|---|---|
| Select vector elements | x[c(1, 3)] |
R positions start at 1. |
| Exclude vector elements | x[-2] |
Do not mix positive and negative positions, except zero. |
| Select by a condition | x[x > 10] |
The condition produces a logical vector. Handle missing values explicitly. |
| Select a matrix or data-frame rectangle | x[rows, columns] |
The comma separates the two dimensions; a blank selects all entries in that dimension. |
| Keep a one-column table | df[, "height", drop = FALSE] |
Without drop = FALSE, selecting one column this way can return a vector. |
| Select one list component | x[[1]] or x[["name"]] |
x[1] instead returns a list containing that component. |
| Select a named component | df$height |
Convenient for a known column name; use df[[column_name]] if the name is stored in a variable. |
For a data frame, df["height"] selects a column as a data frame; df[1, ] selects a row. For a matrix, a single index such as m[3] counts through columns. Always consider the object’s structure before interpreting brackets.
which() returns positions that meet a condition. %in% tests membership, while match() finds positions of matches. Use &, |, and ! for element-by-element AND, OR, and NOT. Test equality with ==; <- assigns a value.
A short recap in R
observations <- data.frame(
site = c("A", "B", "C", "D"),
height_cm = c(12, NA, 18, 9)
)
keep <- !is.na(observations$height_cm) & observations$height_cm >= 12
selected <- observations[keep, c("site", "height_cm"), drop = FALSE]
selected
The result contains sites A and C, with heights 12 and 18 cm. The explicit !is.na() check excludes the missing measurement. A comparison with an NA alone can leave NA entries in a logical index and produce missing rows in the result.
You can also put an indexed expression on the left of <- to replace selected values. Check the selection first so that you change the intended entries. subset() provides another way to select rows and columns; sample() selects a random sample. Use set.seed() when an example’s random selection needs to be reproducible.
Check your understanding
- Explain the difference between
observations[1, ]andobservations[, 1]. - How would you select only site C using its name rather than its row number?
- Why do
my_list[1]andmy_list[[1]]sometimes give very different results?
Revisit indexing and subsetting for more examples.