A generic function to return the first or last elements or rows of a vector or two-dimensional data object.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |

`x` |
1 or 2 dimensional data object |

`n` |
number of periods to return |

`keep` |
should removed values be kept? |

`...` |
additional args - unused |

Provides the ability to identify the first or last
`n`

rows or observations of a data set. The generic
method behaves much like `head`

and `tail`

from
base, except by default only the *first* or
*last* observation will be returned.

The more useful method for the xts class allows for time based subsetting, given an xtsible object.

`n`

may be either a numeric value, indicating the number of
observations to return - forward from `first`

, or backwards from `last`

,
or it may be a character string describing the number and type of periods to
return.

`n`

may be positive or negative, in either numeric or character
contexts. When positive it will return the result expected - e.g.
`last(X,'1 month')`

will return the last month's data. If negative,
all data will be returned *except* for the last month. It is important
to note that this is not the same as calling `first(X,'1 month')`

or
`first(X,'-1 month')`

. All 4 variations return different subsets of
data and have distinct purposes.

If `n`

is a character string, it must be of the form ‘n period.type’
or ‘period.type’,
where `n`

is a numeric value (defaults to 1 if not provided)
describing the number of `period.types`

to move forward (first) or back (last).

For example, to return the last 3 weeks of a time oriented zoo object, one
could call `last(X,'3 weeks')`

. Valid period.types are: secs, seconds,
mins, minutes, hours, days, weeks, months, quarters, and years.

It is possible to use any frequency specification (secs, mins, days, ...) for the period.type portion of the string, even if the original data is in a higher frequency. This makes it possible to return the last ‘2 months’ of data from an oject that has a daily periodicity.

It should be noted that it is only possible to extract data with methods equal to or less than the frequency of the original data set. Attempting otherwise will result in error.

Requesting more data than is in the original data object will produce a warning advising as such, and the object returned will simply be the original data.

A subset of elements/rows of the original data.

Jeffrey A. Ryan

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.