Member-only story

Mastering R: 100 Different Ways for Data Selection and Indexing

btd
9 min readDec 14, 2023

--

Data selection and indexing refer to the process of extracting specific elements, subsets, or columns from a data structure, such as a vector, matrix, data frame, list, or array. This is a fundamental operation in data analysis and manipulation, allowing you to focus on relevant portions of your data for further exploration, analysis, or visualization.

I. DATA FRAMES:

1. Numeric Indexing:

df[1, 2]  # Selects element in the first row and second column

2. Column Selection by Name:

df$columnName  # Selects the entire column by name

3. Subset by Condition:

df[df$column > 10, ]  # Selects rows where a specific condition is met

4. Select Columns by Index:

df[, c(1, 3)]  # Selects specific columns by index

5. Subset by Logical Condition:

df[df$column == "value", ]  # Selects rows based on logical condition

6. Select Columns by Name:

df[, c("col1", "col2")]  # Selects specific columns by name

--

--

btd
btd

No responses yet