Skip to contents

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, ...)

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