Using select

The verb select, which is part of the dplyr package (installed automatically when the tidyverse package is installed), can be used to select and rename columns from a dataset, as follows:

# dplyr Verbs # select # Add the state name to the tstate dataset tstate$Name <- state.name select(tstate, Income, Frost, Area) # selecting specific columns # # A tibble: 50 x 3 # Income Frost Area # <dbl> <dbl> <dbl> # 1 3624 20 50708 # 2 6315 152 566432 select(tstate, Population:Illiteracy) # selecting a range of columns # # A tibble: 50 x 3 # Population Income Illiteracy # <dbl> <dbl> <dbl> # 1 3615 3624 2.1 # 2 365 6315 1.5 # 3 2212 4530 1.8 select(tstate, -c(Population:Illiteracy)) # excluding a range of columns # # A tibble: 50 x 7 # `Life ...

Get Hands-On Data Science with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.