Learn R Programming

timelineS (version 0.1.1)

durCalc: Filter Dataset by Date Duration

Description

Calculates the duration between two dates, use it as a filter to select rows that satisfy the length criteria. Returns the dataset with additional columns regarding the length of durations in different units.

Usage

durCalc(df = NULL, start, end, timeunit = "day", filterlength = NA, filterlonger = TRUE, year = 365.25, month = 30.42)

Arguments

df
Data frame containing start and end dates.
start
Column in df for start dates or a date to use as start date.
end
Column in df for end dates or a date to use as a end date.
timeunit
Unit of time to be used in plots. "day(s)", "week(s)", "month(s)", "quarter(s)", "semiannual", "halfyear",or "year(s)".
filterlength
A time length to use as filter.
filterlonger
If TRUE, the function gives rows with longer durations than specified in filterlength. If FALSE, gives rows with shorter durations.
year
Number of days to use as a year. Default is 365.25.
month
Number of days to use as a month. Default is 30.42.

Value

A subset of original data frame with additional columns in specified time units and calendar units.

Details

Additional columns returned with the filtered rows are: length of duration in days, in specified time unit, and in calendar units, and how much longer/shorter the durations are compared to filter length in calendar units.

If no filterlength is provided, then returns all rows with length of duration in days and calendar units.

You can use dates for start and end and provide no df to get the length of duration between the dates in calendar units. See example.

See Also

durPlot, durSummary

Examples

Run this code
### Filter people who lived longer than 85 years
durCalc(life_exp, start = "Birth", end = "Death", timeunit = "years", filterlength = 85)

### How old each person would be as of January 1, 2000 if they were alive
durCalc(life_exp, start = "Birth", end = as.Date("2000-1-1"), timeunit = "years")

### Use as unit-converter between two dates
durCalc(start = as.Date("2010-12-1"), end = as.Date("2015-4-26"), timeunit = "weeks")

Run the code above in your browser using DataLab