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
.