Get Started

Arranging rows

Back in the days when music was stored on CDs, there was a perennial problem: how do you best order your CDs so you can find the ones you want? By order of artist? Chronologically? By genre?

The arrange() function lets you reorder the rows of a tibble. It takes a tibble, followed by the unquoted names of columns. For example, to sort in ascending order of the values of column x, then (where there is a tie in x) by descending order of values of y, you would write the following.

a_tibble %>%
  arrange(x, desc(y))

Notice the use of desc() to enforce sorting by descending order. Also be aware that in sparklyr, the order() function, used for arranging the rows of data.frames does not work.

This is a part of the course

“Introduction to Spark with sparklyr in R”

View Course

Exercise instructions

A Spark connection has been created for you as spark_conn. A tibble attached to the track metadata stored in Spark has been pre-defined as track_metadata_tbl.

  • Select the artist_name, release, title, and year fields.
  • Pipe the result of this to filter on tracks from the 1960s.
  • Pipe the result of this to arrange() to order by artist_name, then descending year, then title.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# track_metadata_tbl has been pre-defined
track_metadata_tbl

# Manipulate the track metadata
track_metadata_tbl %>%
  # Select columns
  ___ %>%
  # Filter rows
  ___ %>%
  # Arrange rows
  ___
Edit and Run Code

This exercise is part of the course

Introduction to Spark with sparklyr in R

IntermediateSkill Level
5.0+
4 reviews

Learn how to run big data analysis using Spark and the sparklyr package in R, and explore Spark MLIb in just 4 hours.

In which you learn how Spark and R complement each other, how to get data to and from Spark, and how to manipulate Spark data frames using dplyr syntax.

Exercise 1: Getting startedExercise 2: Made for each otherExercise 3: Here be dragonsExercise 4: The connect-work-disconnect patternExercise 5: Copying data into SparkExercise 6: Big data, tiny tibbleExercise 7: Exploring the structure of tibblesExercise 8: Selecting columnsExercise 9: Filtering rowsExercise 10: Arranging rows
Exercise 11: Mutating columnsExercise 12: Summarizing columns

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free