简体   繁体   English

R中多个日期的difftime

[英]difftime for multiple dates in r

I have chemistry water data taken from a river. 我有从河中提取的化学水数据。 Normally, the sample dates were on a Wednesday every two weeks. 通常,采样日期是每两周的星期三。 The data record starts in 1987 and ends in 2013. 数据记录始于1987年,结束于2013年。

Now, I want to re-check if there are any inconsistencies within the data, that is if the samples are really taken every 14 days. 现在,我想重新检查数据中是否存在任何不一致,即是否真的每14天取样一次。 For that task I want to use the r function difftime. 对于该任务,我想使用r函数difftime。 But I have no idea on how to do that for multiple dates. 但是我不知道如何多次约会。

Here is some data: 这是一些数据:

Date                     Value
1987-04-16 12:00:00      1,5
1987-04-30 12:00:00      1,2
1987-06-25 12:00:00      1,7
1987-07-14 12:00:00      1,3

Can you tell me on how to use the function difftime properly in that case or any other function that does the job. 您能告诉我如何在这种情况下正确使用difftime或执行此工作的任何其他函数。 The result should be the number of days between the samplings and/or a true and false for the 14 days. 结果应该是两次采样之间的天数和/或14天的真假。

Thanks to you guys in advance. 预先感谢你们。 Any google-fu was to no avail! 任何谷歌傅都无济于事!

Assuming your data.frame is named dd , you'll want to verify that the Date column is being treated as a date. 假设data.frame名为dd ,则需要验证Date列是否被视为日期。 Most times R will read them as a character which gets converted to a factor in a data.frame. 大多数情况下,R会将它们作为字符读取,该字符将转换为data.frame中的因子。 If class(df$Date) is "character" or "factor", run 如果class(df$Date)是“ character”或“ factor”,请运行

dd$Date<-as.POSIXct(as.character(dd$Date), format="%Y-%m-%d %H:%M:%S")

Then you can so a simple diff() to get the time difference in days 然后,您可以使用一个简单的diff()来获取以天为单位的时差

diff(dd$Date)
# Time differences in days
# [1] 14 56 19
# attr(,"tzone")
# [1] ""

so you can check which ones are over 14 days. 因此您可以检查哪些超过14天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM