我有一個df調用t_compl
三列日期。如何計算不同的日期列
date_op_indl date_compl usdato.x
1984-11-22 1984-11-22 1983-09-07
2004-11-16 NA 1994-10-27
1996-09-10 1996-09-10 1982-11-09
1986-05-24 1986-05-24 1982-11-22
1989-08-22 1989-06-13 1983-02-11
我想創建一個第四個變量(t_compl$time
),在幾年usdato.x
和date_compl
或date_op_indl
之間的時間差。我想從date_compl
和date_op_indl
中選擇最早的日期。
我已經試過
t_compl$time<- ifelse((t_compl$date_compl-t_compl$usdato.x)<=(t_compl$date_op_indl-t_compl$usdato.x), ((t_compl$date_compl-t_compl$usdato.x)/365.25), ((t_compl$date_op_indl-t_compl$usdato.x)/365.25))
因爲一些date_compl
缺少我想用compl_op_indl
用於計算
t_compl$time[is.na(t_compl$time)] <- ((t_compl$date_op_indl-t_compl$usdato.x)/365.25)
,然後得到這個錯誤
Warning message:
In t_compl$time[is.na(t_compl$time)] <- ((t_compl$date_op_indl - :
number of items to replace is not a multiple of replacement length
的時間計算還對於完全錯誤3210是NA
。
如何在R中進行時差計算?
你想要結果爲整年或年 - 月 - 日嗎? 'difftime'對你來說可能非常有用。 – 2014-10-20 16:21:06
我需要時間單位的確切時間差。我選擇了幾年,因爲我有一個30年的觀察期。 – Daniel 2014-10-20 16:26:05