Selecting and renaming columns

The primary way to select columns in dplyr is by using the select function. At its simplest, select just takes the tibble (or data.table or data.frame, and so on) to operate on and an arbitrary number of columns. But, as we’ll see, select has a few tricks up its sleeve and is far more powerful than it seems, as shown in the following code:

> # select trackname and artist> tracks %>% select(trackname, artist)# A tibble: 138,135 x 2   trackname                                artist   <chr>                                    <chr>  1 A Perfect Match                          A*Teens 2 Mamma Mia                                A*Teens 3 Upside Down                              A*Teens…# ... with 138,125 more rows>> # same thing using the standard-evaluation version> tracks %>% select_(“trackname”, “artist”)# A tibble: 138,135 x 2   trackname                                artist   <chr>                                    <chr>  1 A Perfect ...

Get Data Analysis with R - Second Edition 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.