library(tidyverse)
library(janitor)

DPLYR

Using select helpers

When working with LONG variable names use starts with, contains, or ends with to select (or rename, summarise) variables based on part of the name.

# get some data
iris_demo <- iris %>%
  clean_names()

petal <- iris_demo %>%
  select(starts_with("petal"), species)

width <- iris_demo %>%
  select(ends_with("width"), species)

Using mutate_at with select helpers

mutate_at allows you to select a certain range of columns and apply the same change to all of them.

There are lots of helpful tutorials by R-Ladies

  1. Suzan Baert
  2. Rebecca Barter
  3. my notes
Real Example 1

Students who have not completed a module (i.e data is NA) get 0. Replace all the NAs with 0 in columns starting with “Media”

mutate_at(vars(contains("Media")), ~ replace(., is.na(.), 0))
Real Example 2

Prep work scores coming from different platforms sometimes read in as characters other times numeric. Make them all numeric.

mutate_at(vars(starts_with("prep")), funs(as.numeric(.))) 
Real Example 3

Scoring psych scales, need to recode all scores from a particular subscale 0, 1, 2.

mutate_at(vars(starts_with("itsea_section_a_")), ~ recode(., 
                                        "Not True/Rarely" = "0",
                                 "Somewhat True/Sometimes" = "1",
                                      "Very True/Often" = "2",
                                            .default = "NA"))

Dplyr 1.0.0

Watch out for the update that is coming to dplyr. The function across() that will make the _at, _if, _all functions unnecessary. Read about it here