Add a second to duplicated POSIXct dates
dataframe, posixct, r
Solution
You can use `make.index.unique` in the xts package.
x <- structure(list(value = c(18, 62, 64, 29, 22),
date = structure(c(1373406563, 1373410896, 1373413611, 1373413611, 1373413611),
class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("value", "date"),
row.names = c(NA, -5L), class = "data.frame")
x$date.unique <- make.index.unique(x$date,1)
x
# value date date.unique
# 1 18 2013-07-09 16:49:23 2013-07-09 16:49:23
# 2 62 2013-07-09 18:01:36 2013-07-09 18:01:36
# 3 64 2013-07-09 18:46:51 2013-07-09 18:46:51
# 4 29 2013-07-09 18:46:51 2013-07-09 18:46:52
# 5 22 2013-07-09 18:46:51 2013-07-09 18:46:53
Problem
I am trying to add single seconds to any repeated dates in my data frame. i.e. from this: ``` value date 18 2013-07-09 16:49:23 62 2013-07-09 18:01:36 64 2013-07-09 18:46:51 29 2013-07-09 18:46:51 22 2013-07-09 18:46:51 .... ``` I would like to obtain this: ``` value date 18 2013-07-09 16:49:23 62 2013-07-09 18:01:36 64 2013-07-09 18:46:51 29 2013-07-09 18:46:52 22 2013-07-09 18:46:53 .... ``` I understand I can simply add + 1 or +2 to the POSIXct format to add seconds- however I don't know how to select the duplicates. Note that my dataframe is a few hundred of rows long, and a date can appear up to 20 times in a row. I am thinking of doing something along these lines: ``` for (item in duplicated(dataframe$date)) { if (item == TRUE) { for (n in 1:#length of duplicated dates) { dataframe[index(item) +n]$date <- (dataframe[index(item) +n]$date +n) } } } ``` Thank you for your help!