Generic functions to return the first or last elements or rows of a vector or two-dimensional data object.
Usage
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, ...)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.
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]
#> 2026-09-29 1
#> 2026-09-30 2
#> 2026-10-01 3
#> 2026-10-02 4
#> 2026-10-03 5
#> 2026-10-04 6
#> 2026-10-05 7
#> 2026-10-06 8
#> 2026-10-07 9
#> 2026-10-08 10
first(x, '1 day')
#> [,1]
#> 2026-09-29 1
first(x, '4 days')
#> [,1]
#> 2026-09-29 1
#> 2026-09-30 2
#> 2026-10-01 3
#> 2026-10-02 4
first(x, 'month')
#> [,1]
#> 2026-09-29 1
#> 2026-09-30 2
last(x, '2 months')
#> [,1]
#> 2026-12-01 64
#> 2026-12-02 65
#> 2026-12-03 66
#> 2026-12-04 67
#> 2026-12-05 68
#> 2026-12-06 69
#> 2026-12-07 70
#> 2026-12-08 71
#> 2026-12-09 72
#> 2026-12-10 73
#> 2026-12-11 74
#> 2026-12-12 75
#> 2026-12-13 76
#> 2026-12-14 77
#> 2026-12-15 78
#> 2026-12-16 79
#> 2026-12-17 80
#> 2026-12-18 81
#> 2026-12-19 82
#> 2026-12-20 83
#> 2026-12-21 84
#> 2026-12-22 85
#> 2026-12-23 86
#> 2026-12-24 87
#> 2026-12-25 88
#> 2026-12-26 89
#> 2026-12-27 90
#> 2026-12-28 91
#> 2026-12-29 92
#> 2026-12-30 93
#> 2026-12-31 94
#> 2027-01-01 95
#> 2027-01-02 96
#> 2027-01-03 97
#> 2027-01-04 98
#> 2027-01-05 99
#> 2027-01-06 100
last(x, '6 weeks')
#> [,1]
#> 2026-11-30 63
#> 2026-12-01 64
#> 2026-12-02 65
#> 2026-12-03 66
#> 2026-12-04 67
#> 2026-12-05 68
#> 2026-12-06 69
#> 2026-12-07 70
#> 2026-12-08 71
#> 2026-12-09 72
#> 2026-12-10 73
#> 2026-12-11 74
#> 2026-12-12 75
#> 2026-12-13 76
#> 2026-12-14 77
#> 2026-12-15 78
#> 2026-12-16 79
#> 2026-12-17 80
#> 2026-12-18 81
#> 2026-12-19 82
#> 2026-12-20 83
#> 2026-12-21 84
#> 2026-12-22 85
#> 2026-12-23 86
#> 2026-12-24 87
#> 2026-12-25 88
#> 2026-12-26 89
#> 2026-12-27 90
#> 2026-12-28 91
#> 2026-12-29 92
#> 2026-12-30 93
#> 2026-12-31 94
#> 2027-01-01 95
#> 2027-01-02 96
#> 2027-01-03 97
#> 2027-01-04 98
#> 2027-01-05 99
#> 2027-01-06 100