O'Reilly logo

Data Analysis with R - Second Edition by Tony Fischetti

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Manipulating rows

As we did in the last section, let’s start in earnest by learning how to subset our data by condition. For this, we use dplyr’s filter function which takes an expression that returns a logical vector:

> tracks %>% filter(artist=="Belle and Sebastian")> tracks %>% filter(artist=="Belle and Sebastian" | artist=="The Smiths")> tracks %>% filter(artist %in% c(“Belle and Sebastian", "The Smiths"))> tracks %>% filter(artist=="Belle and Sebastian" & trackname=="The Model")

Really simple stuff so far!

Where we previously used the order function in the i field to sort by a column, the dplyr way is to use the arrange function:

> tracks ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required