0
0
R Programmingprogramming~5 mins

select() for column selection in R Programming

Choose your learning style9 modes available
Introduction

The select() function helps you pick specific columns from a table or data frame. It makes your data easier to work with by focusing only on the columns you need.

You have a big table but want to work with only a few columns.
You want to reorder columns to make your data easier to read.
You want to rename columns while selecting them.
You want to drop some columns and keep the rest.
You want to select columns based on a pattern or condition.
Syntax
R Programming
select(data_frame, column1, column2, ...)

You need to load the dplyr package to use select().

You can use helper functions like starts_with(), ends_with(), or contains() inside select() to pick columns by name patterns.

Examples
Selects only the name and age columns from df.
R Programming
select(df, name, age)
Selects all columns whose names start with the letter "a".
R Programming
select(df, starts_with("a"))
Selects the age column and renames height_cm to height.
R Programming
select(df, age, height = height_cm)
Selects all columns except the weight column.
R Programming
select(df, -weight)
Sample Program

This program creates a small table of people with their name, age, height, and weight. Then it uses select() to keep only the name and age columns. Finally, it prints the result.

R Programming
library(dplyr)

# Create a sample data frame
people <- data.frame(
  name = c("Alice", "Bob", "Carol"),
  age = c(25, 30, 22),
  height_cm = c(165, 180, 170),
  weight_kg = c(55, 80, 60)
)

# Select only name and age columns
selected_data <- select(people, name, age)

print(selected_data)
OutputSuccess
Important Notes

If you forget to load dplyr, select() won't work.

You can use negative signs to drop columns instead of picking them.

Column names are not quoted inside select().

Summary

select() helps you pick or drop columns from a data frame.

You can use it to rename columns while selecting.

It works best when you want to focus on specific parts of your data.