Return the First or Last Part of an Object

Returns the first or last parts of a vector, matrix, table, data frame or function. Since head() and tail() are generic functions, they may also have been extended to other classes.

head(x, ...) "head"(x, n = 6L, ...) "head"(x, n = 6L, ...) "head"(x, n = 6L, ...) "head"(x, n = 6L, ...) "head"(x, n = 6L, ...) "head"(x, n = 6L, ...)
tail(x, ...) "tail"(x, n = 6L, ...) "tail"(x, n = 6L, ...) "tail"(x, n = 6L, addrownums = TRUE, ...) "tail"(x, n = 6L, addrownums = FALSE, ...) "tail"(x, n = 6L, addrownums = TRUE, ...) "tail"(x, n = 6L, ...)
an object
a single integer. If positive, size for the resulting object: number of elements for a vector (including lists), rows for a matrix or data frame or lines for a function. If negative, all but the n last/first number of elements of x.
if there are no row names, create them from the row numbers.
arguments to be passed to or from other methods.

For matrices, 2-dim tables and data frames, head() (tail()) returns the first (last) n rows when n > 0 or all but the last (first) n rows when n < 0. head.matrix() and tail.matrix() are exported. For functions, the lines of the deparsed function are returned as character strings.

If a matrix has no row names, then tail() will add row names of the form "[n,]" to the result, so that it looks similar to the last lines of x when printed. Setting addrownums = FALSE suppresses this behaviour.


An object (usually) like x but generally smaller. For ftable objects x, a transformed format(x).

  • head
  • head.default
  • head.data.frame
  • head.function
  • head.ftable
  • head.table
  • head.matrix
  • tail
  • tail.default
  • tail.data.frame
  • tail.function
  • tail.ftable
  • tail.table
  • tail.matrix
library(utils) head(letters) head(letters, n = -6L) head(freeny.x, n = 10L) head(freeny.y) tail(letters) tail(letters, n = -6L) tail(freeny.x) tail(freeny.y) tail(library) head(stats::ftable(Titanic))
Documentation reproduced from package utils, version 3.2.4, License: Part of R 3.2.4

Community examples

Looks like there are no examples yet.