1.7 Selecting variables
The dplyr::select
function allows you to limit your dataset to specified variables (columns).
library(dplyr)
data("starwars")
# keep the variables name, height, and gender
newdata <- select(starwars, name, height, gender)
# keep the variables name and all variables
# between mass and species inclusive
newdata <- select(starwars, name, mass:species)
# keep all variables except birth_year and gender
newdata <- select(starwars, -birth_year, -gender)