I have 12 dates and times in a table in POSIXct format:
Time
1 2017-03-11 01:10:09
2 2017-03-11 03:07:58
3 2017-03-12 19:16:47
4 2017-03-13 09:52:04
5 2017-03-17 20:36:35
6 2017-03-18 03:10:54
7 2017-03-18 07:29:31
8 2017-03-18 10:13:37
9 2017-03-20 10:19:31
10 2017-03-20 12:11:39
11 2017-03-20 12:11:39
12 2017-03-20 14:16:12
If an entry matches the following entry, I want to remove one second from the time. For example, row 10 should appear as "2017-03-20 12:11:38".
I'm trying to do something like the following but that works:
df %>% mutate(Time = ifelse(Time == lead(Time), Time-1, Time))
Your approach is correct however,
base::ifelsestrips off the attribute makingPOSIXctcolumn as numeric. To avoid that since you are usingdplyryou can usedplyr::if_elsewhich preserves the class.Also note that
leadwill generateNAfor last value inTimeso the comparisonTime == lead(Time)would generateNA. We could setmissingargument asTimeinif_else.