Generic functions to return the first or last elements or rows of a vector or two-dimensional data object.

first(x, ...)

# Default S3 method
first(x, n = 1, keep = FALSE, ...)

# S3 method for class 'xts'
first(x, n = 1, keep = FALSE, ...)

last(x, ...)

# Default S3 method
last(x, n = 1, keep = FALSE, ...)

# S3 method for class 'xts'
last(x, n = 1, keep = FALSE, ...)

Arguments

x

An object.

...

Arguments passed to other methods.

n

Number of observations to return.

keep

Should removed values be kept as an attribute on the result?

Value

A subset of elements/rows of the original data.

Details

A more advanced subsetting is available for zoo objects with indexes inheriting from POSIXt or Date classes.

Quickly and easily extract the first or last n observations of an object. When n is a number, these functions are similar to head() and tail(), but only return the first or last observation by default.

n can be a character string if x is an xts object or coerceable to xts. It must be of the form ‘n period’, where 'n' is a numeric value (1 if not provided) describing the number of periods to return. Valid periods are: secs, seconds, mins, minutes, hours, days, weeks, months, quarters, and years.

The 'period' portion can be any frequency greater than or equal to the frequency of the object's time index. For example, first(x, "2 months") will return the first 2 months of data even if x is hourly frequency. Attempts to set 'period' to a frequency less than the object's frequency will throw an error.

n may be positive or negative, whether it's a number or character string. When n is positive, the functions return the obvious result. For example, first(x, "1 month") returns the first month's data. When n is negative, all data except first month's is returned.

Requesting more data than is in x will throw a warning and simply return x.

Author

Jeffrey A. Ryan

Examples


first(1:100)
#> [1] 1
last(1:100)
#> [1] 100

data(LakeHuron)
first(LakeHuron,10)
#>  [1] 580.38 581.86 580.97 580.80 579.79 580.39 580.42 580.82 581.40 581.32
last(LakeHuron)
#> [1] 579.96

x <- xts(1:100, Sys.Date()+1:100)
first(x, 10)
#>            [,1]
#> 2025-11-01    1
#> 2025-11-02    2
#> 2025-11-03    3
#> 2025-11-04    4
#> 2025-11-05    5
#> 2025-11-06    6
#> 2025-11-07    7
#> 2025-11-08    8
#> 2025-11-09    9
#> 2025-11-10   10
first(x, '1 day')
#>            [,1]
#> 2025-11-01    1
first(x, '4 days')
#>            [,1]
#> 2025-11-01    1
#> 2025-11-02    2
#> 2025-11-03    3
#> 2025-11-04    4
first(x, 'month')
#>            [,1]
#> 2025-11-01    1
#> 2025-11-02    2
#> 2025-11-03    3
#> 2025-11-04    4
#> 2025-11-05    5
#> 2025-11-06    6
#> 2025-11-07    7
#> 2025-11-08    8
#> 2025-11-09    9
#> 2025-11-10   10
#> 2025-11-11   11
#> 2025-11-12   12
#> 2025-11-13   13
#> 2025-11-14   14
#> 2025-11-15   15
#> 2025-11-16   16
#> 2025-11-17   17
#> 2025-11-18   18
#> 2025-11-19   19
#> 2025-11-20   20
#> 2025-11-21   21
#> 2025-11-22   22
#> 2025-11-23   23
#> 2025-11-24   24
#> 2025-11-25   25
#> 2025-11-26   26
#> 2025-11-27   27
#> 2025-11-28   28
#> 2025-11-29   29
#> 2025-11-30   30
last(x, '2 months')
#>            [,1]
#> 2026-01-01   62
#> 2026-01-02   63
#> 2026-01-03   64
#> 2026-01-04   65
#> 2026-01-05   66
#> 2026-01-06   67
#> 2026-01-07   68
#> 2026-01-08   69
#> 2026-01-09   70
#> 2026-01-10   71
#> 2026-01-11   72
#> 2026-01-12   73
#> 2026-01-13   74
#> 2026-01-14   75
#> 2026-01-15   76
#> 2026-01-16   77
#> 2026-01-17   78
#> 2026-01-18   79
#> 2026-01-19   80
#> 2026-01-20   81
#> 2026-01-21   82
#> 2026-01-22   83
#> 2026-01-23   84
#> 2026-01-24   85
#> 2026-01-25   86
#> 2026-01-26   87
#> 2026-01-27   88
#> 2026-01-28   89
#> 2026-01-29   90
#> 2026-01-30   91
#> 2026-01-31   92
#> 2026-02-01   93
#> 2026-02-02   94
#> 2026-02-03   95
#> 2026-02-04   96
#> 2026-02-05   97
#> 2026-02-06   98
#> 2026-02-07   99
#> 2026-02-08  100
last(x, '6 weeks')
#>            [,1]
#> 2025-12-29   59
#> 2025-12-30   60
#> 2025-12-31   61
#> 2026-01-01   62
#> 2026-01-02   63
#> 2026-01-03   64
#> 2026-01-04   65
#> 2026-01-05   66
#> 2026-01-06   67
#> 2026-01-07   68
#> 2026-01-08   69
#> 2026-01-09   70
#> 2026-01-10   71
#> 2026-01-11   72
#> 2026-01-12   73
#> 2026-01-13   74
#> 2026-01-14   75
#> 2026-01-15   76
#> 2026-01-16   77
#> 2026-01-17   78
#> 2026-01-18   79
#> 2026-01-19   80
#> 2026-01-20   81
#> 2026-01-21   82
#> 2026-01-22   83
#> 2026-01-23   84
#> 2026-01-24   85
#> 2026-01-25   86
#> 2026-01-26   87
#> 2026-01-27   88
#> 2026-01-28   89
#> 2026-01-29   90
#> 2026-01-30   91
#> 2026-01-31   92
#> 2026-02-01   93
#> 2026-02-02   94
#> 2026-02-03   95
#> 2026-02-04   96
#> 2026-02-05   97
#> 2026-02-06   98
#> 2026-02-07   99
#> 2026-02-08  100