Rearranging data
Sometimes, we do not want to filter any part of the data (neither the rows, nor the columns), but the data is simply not in the most useful order due to convenience or performance issues, as we have seen, for instance, in Chapter 3, Filtering and Summarizing Data.
Besides the base sort
and order
functions, or providing the order of variables passed to the [
operator, we can also use some SQL-like solutions with the sqldf
package, or query the data in the right format directly from the database. And the previously mentioned dplyr
package also provides an effective method for ordering data. Let's sort the hflights
data, based on the actual elapsed time for each of the quarter million flights:
> str(arrange(hflights, ActualElapsedTime)) ...
Get Mastering Data Analysis 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.