1

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.

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. For that task I want to use the r function 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. The result should be the number of days between the samplings and/or a true and false for the 14 days.

Thanks to you guys in advance. Any google-fu was to no avail!

Strohmi
  • 523
  • 5
  • 21
  • What class is your date data? `POSIXt`? Hard to tell what to do without a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Looks like you could use `diff` on your date column. An all-in solution may be: `all(diff(diff(dat$Date)) == 0L)` – Blue Magister May 20 '14 at 14:49
  • Or alternatively `table(diff(dat$Date))` might be more useful to get an idea of the distribution of sample intervals. – Miff May 20 '14 at 15:04

1 Answers1

1

Assuming your data.frame is named dd, you'll want to verify that the Date column is being treated as a date. Most times R will read them as a character which gets converted to a factor in a data.frame. If class(df$Date) is "character" or "factor", run

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(dd$Date)
# Time differences in days
# [1] 14 56 19
# attr(,"tzone")
# [1] ""

so you can check which ones are over 14 days.

MrFlick
  • 195,160
  • 17
  • 277
  • 295