library(tidyverse)
library(janitor)
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)
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
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))
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(.)))
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"))
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