The “select” Function in R
Package: dplyr
Purpose: Selects columns from data frames.
General Class: Data Manipulation
Required Argument(s):
.data: A data frame or tibble.
...: Column names or expressions to select.
Notable Optional Arguments:
starts_with, ends_with, contains, matches: Helper functions to select columns based on name patterns.
rename: Rename selected columns.
across: Apply a function to select columns.
everything: Select all columns.
Example:
# Example data for selecting columns using dplyr
library(dplyr)
df <- data.frame(
ID = 1:5,
Name = c("Alice", "Bob", "Charlie", "David", "Emma"),
Age = c(25, 30, 22, 28, 35)
)
# Select only the "ID" and "Name" columns
selected_df <- select(df, ID, Name)
# Display the selected data frame
print(selected_df)In this example, the select function from the dplyr package is used to choose specific columns (“ID” and “Name”) from a data frame (df). The result is a new data frame (selected_df) containing only the selected columns. Note that you need to have the dplyr package installed and loaded to use the select function.