year_frac: Converting Time Indices to Year Fractions and Back

View source: R/calendar.R

year_fracR Documentation

Converting Time Indices to Year Fractions and Back

Description

year_frac computes year fraction corresponding to a time index. yf2tind performs the reverse computation.

Usage

year_frac(x)

yf2tind(x, type = NULL, tz = NULL)

Arguments

x

an object of tind class or an R object coercible to it for year_frac, a numeric vector for yf2tind.

type

a character value determining time index type (y - years, q - quarters, m - months, w - weeks, d - dates, t - date-time).

tz

(optional) a character value determining the time zone (the default NULL is interpreted as the system time zone). See tzone documentation for information on time zones.

Details

year_fraction returns numeric vector representing time indices in the form year + year fraction. For years this is equivalent to as.numeric. Year fraction is determined based on the index within particular year (minus 1 for all indices except for date-time) and the number of periods within the year. E.g., 2001Q1 gives 2001 (2001 + (1 - 1) / 4), 2001Q32001.5 (2001 + (3 - 1) / 4), 2010-04 (April 2010) — 2010.25 (2010 + (4 - 1) / 12), 2000-02-29 (60th day in 2000) — 2000.1612 (2000 + (60 - 1) / 366, 2000 was a leap year).

Value

A numeric vector for year_frac and tind for yf2tind. Result is of the same length as argument.

Note

year_frac is not to be confused with a similarly called function (YEARFRAC) found in popular spreadsheet software. In order to compute differences between dates as year fractions use daycount_frac function.

See Also

daycount_frac.

Examples

year_frac(today())
year_frac(now())
yf2tind(2023.5, "y")
yf2tind(2023.5, "q")
yf2tind(2023.5, "m")
yf2tind(2023.5, "w")
yf2tind(2023.5, "d")
yf2tind(2023.5, "t")


tind documentation built on Dec. 28, 2025, 1:06 a.m.