Head or Tail

We wish to obtain the top n or bottom n rows of a data frame. It is good practice to look at the top and bottom set of rows of a new data frame especially when reading a spreadsheet export. Typically encoding issues and non data content (e.g. comments) can be seen by inspecting those top or bottom n rows.

We wish to get the top n rows of a data frame.

df %>% slice_head(n=10)

Here is how this works:

  • We pass the Data Frame df to the function slice_head().
  • To set the number of rows we wish to get from the top (head) of a data frame, we use the n argument of the slice_head() function. In this example we set n=10 to obtain the 10 first rows of the data frame. slice_head() returns 1 row by default.
  • We recommend dplyr’s more powerful slice_head() over base R’s head(). One reason is that slice_head() works on grouped data frames but head() doesn’t.

Tail

We wish to get the bottom n rows of a data frame.

df %>% slice_tail(n=10)

This code works similarly to getting the top n rows described above.

Selected Columns

We wish to get the top / bottom n rows of a data frame but return only a selected set of columns.

df %>% select(col_1, col_3) %>% slice_head(n=10)

Here is how this works:

  • We use select() to specify the column names of the columns of the data frame df that we wish to include in the output. In this example, the column names are col_1 and col_3. For a detailed coverage, see Selecting by Name.
  • We then pass the output of select() to slice_head(n=10) to get the first ten rows.
R
I/O