在 R 编程中计算日期之间的时间差 - difftime()函数
R 语言中的difftime()
函数用于以所需的单位(如天、周、月、年等)计算日期之间的时间差。
Syntax: difftime(date1, date2, units)
Parameters:
date1, date2: Dates to calculate difference
units: Days, weeks, months, etc.
示例 1:
# R program to find time difference
# Calling difftime() function
difftime("2020-5-16", "2020-1-15", units = "days")
difftime("2020-5-16", "2020-1-15", units = "weeks")
输出:
Time difference of 122 days
Time difference of 17.42857 weeks
示例 2:
# R program to find time difference
# Calling difftime() function
difftime("2020-5-16", "2020-1-15", units = "hours")
difftime("2020-5-16", "2020-1-15", units = "mins")
输出:
Time difference of 2928 hours
Time difference of 175680 mins