[英]Calculate number of days between two dates in r
I need to calculate the number of days elapsed between multiple dates in two ways and then output those results to new columns: i) number of days that has elapsed as compared to the first date (eg, RESULTS$FIRST) and ii) between sequential dates (eg, RESULTS$BETWEEN). 我需要以两种方式计算多个日期之间经过的天数,然后将这些结果输出到新列:i)与第一个日期相比已经过的天数(例如,结果$ FIRST)和ii)顺序之间日期(例如,结果$ BETWEEN)。 Here is an example with the desired results. 这是一个具有所需结果的示例。 Thanks in advance. 提前致谢。
library(lubridate)
DATA = data.frame(DATE = mdy(c("7/8/2013", "8/1/2013", "8/30/2013", "10/23/2013",
"12/16/2013", "12/16/2015")))
RESULTS = data.frame(DATE = mdy(c("7/8/2013", "8/1/2013", "8/30/2013", "10/23/2013",
"12/16/2013", "12/16/2015")),
FIRST = c(0, 24, 53, 107, 161, 891), BETWEEN = c(0, 24, 29, 54, 54, 730))
#Using dplyr package
library(dplyr)
df1 %>% # your dataframe
mutate(BETWEEN0=as.numeric(difftime(DATE,lag(DATE,1))),BETWEEN=ifelse(is.na(BETWEEN0),0,BETWEEN0),FIRST=cumsum(as.numeric(BETWEEN)))%>%
select(-BETWEEN0)
DATE BETWEEN FIRST
1 2013-07-08 0 0
2 2013-08-01 24 24
3 2013-08-30 29 53
4 2013-10-23 54 107
5 2013-12-16 54 161
6 2015-12-16 730 891
This will get you what you want: 这将为您提供您想要的:
d <- as.Date(DATA$DATE, format="%m/%d/%Y")
first <- c()
for (i in seq_along(d))
first[i] <- d[i] - d[1]
between <- c(0, diff(d))
This uses the as.Date()
function in the base package to cast the vector of string dates to date values using the given format. 这使用基础包中的as.Date()
函数,使用给定的格式将字符串日期的向量as.Date()
为日期值。 Since you have dates as month/day/year, you specify format="%m/%d/%Y"
to make sure it's interpreted correctly. 由于日期为月/日/年,因此请指定format="%m/%d/%Y"
以确保其正确解释。
diff()
is the lagged difference. diff()
是滞后的差异。 Since it's lagged, it doesn't include the difference between element 1 and itself, so you can concatenate a 0. 由于它滞后,它不包括元素1和它自身之间的差异,因此你可以连接0。
Differences between Date
objects are given in days by default. 默认情况下, Date
对象之间的差异以天为单位。
Then constructing the output dataframe is simple: 然后构造输出数据帧很简单:
RESULTS <- data.frame(DATE=DATA$DATE, FIRST=first, BETWEEN=between)
For the first part: 第一部分:
DATA = data.frame((c("7/8/2013", "8/1/2013", "8/30/2013", "10/23/2013","12/16/2013", "12/16/2015")))
names(DATA)[1] = "V1"
date = as.Date(DATA$V1, format="%m/%d/%Y")
print(date-date[1])
Result: 结果:
[1] 0 24 53 107 161 891
For second part - simply use a for
loop 对于第二部分 - 只需使用for
循环
You can just add each column with the simple difftime
and lagged diff
calculations. 您可以使用简单的difftime
和difftime
diff
计算添加每列。
DATA$FIRST <- c(0,
with(DATA,
difftime(DATE[2:length(DATE)],DATE[1], unit="days")
)
)
DATA$BETWEEN <- c(0,
with(DATA,
diff(DATE[1:(length(DATE) - 1)], unit="days")
)
)
identical(DATA, RESULTS)
[1] TRUE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.