How to use everything() in R
Introduction
The everything() function in dplyr is a selection helper that allows you to select all remaining columns in a dataset. It’s particularly useful when you want to reorder columns by moving specific ones to the front while keeping all others, or when you need to apply transformations to all columns at once.
Getting Started
library(tidyverse)
library(palmerpenguins)Example 1: Basic Column Reordering
The Problem
You want to move the most important columns to the front of your dataset while keeping all other columns in their original order.
Step 1: Examine the original data structure
Let’s first look at the structure of our penguins dataset.
penguins |>
head(3) |>
glimpse()This shows us all 8 columns in their default order, with species, island, and bill measurements first.
Step 2: Move specific columns to front
We’ll move the year and sex columns to the beginning while keeping all others.
penguins |>
select(year, sex, everything()) |>
head(3)Now year and sex appear first, followed by all remaining columns in their original order.
Step 3: Reorder multiple columns strategically
Let’s prioritize the outcome variable and key predictors.
penguins |>
select(body_mass_g, species, everything()) |>
head(3)The body mass (our potential outcome) and species now lead, making the data more analysis-ready.
Example 2: Practical Data Transformation
The Problem
You’re preparing data for analysis and need to standardize all numeric columns while preserving categorical variables and maintaining a logical column order for your report.
Step 1: Identify and separate column types
First, let’s move all categorical variables to the front for better data overview.
penguins_reordered <- penguins |>
select(species, island, sex, year, everything())
penguins_reordered |> head(3)This groups related categorical information together at the start of our dataset.
Step 2: Apply transformations using everything()
Now we’ll standardize all numeric columns while keeping categorical ones unchanged.
penguins_standardized <- penguins_reordered |>
mutate(across(where(is.numeric), scale)) |>
select(species, everything())
penguins_standardized |> head(3)All numeric columns are now standardized (mean = 0, sd = 1), while categorical columns remain intact.
Step 3: Create a analysis-ready format
Let’s create a final version with the target variable first and all predictors following.
analysis_data <- penguins |>
select(body_mass_g, everything(), -year) |>
filter(complete.cases(.))
analysis_data |> head(3)We now have body mass as our first column (potential dependent variable), followed by all potential predictors, with incomplete cases removed.
Step 4: Verify the transformation
Check that our reordering and filtering worked as expected.
analysis_data |>
summarise(
n_rows = n(),
n_cols = ncol(.),
first_col = names(.)[1]
)This confirms our data has the expected structure with body_mass_g as the first column and no missing values.
Summary
everything()selects all columns not already mentioned in yourselect()statement- It’s perfect for reordering columns by moving important ones to the front while preserving others
- Combine it with
where()andacross()for powerful data transformations - Use it to create analysis-ready datasets with logical column ordering
It maintains the original order of non-specified columns, making your data transformations predictable