Base R
- subset() function:
subset(df, column_name > value)
- subset operator:
filtered_df <- df[df$column_name > 5, ]
- indices in subset operator:
filtered_df <- df[indices, ]
- which() function to extract indices:
# create a data frame
df <- data.frame(name = c("Alice", "Bob", "Charlie", "David"),
age = c(25, 35, 40, 30))
# use which() to extract indices of rows where age > 30
indices <- which(df$age > 30)
# use the indices to extract the subset of rows
subset_df <- df[indices, ]
# print the resulting data frame
subset_df
Tidyverse/dplyr
- filter() function: filter() function filters rows based on certain conditions.
subset_df <- filter(df, column_name > 30)
subset_df <- filter(df, column1_name > 30
& column2_name == TRUE)
- slice() function: slice() function selects specific rows.
subset_df <- slice(df, indices)
subset_df <- slice(df, c(1,3))
- select() function: select() function selects specific columns.
subset_df <- select(df, column_name)
Top comments (0)