Stack Exchange
set seconds to 0 of xts object
I have a list of xts objects and each object is having varying no. of seconds. How should I set seconds to 0. Here, I am providing one xts object
structure(c(6.00001082143504e-18, 6.00001082143504e-18, 6.00001082143504e-18, 
6.00001082143504e-18, 6.00001082143504e-18, 6.00001082143504e-18
), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", 
"POSIXt"), tzone = "", class = c("xts", "zoo"), index = structure(c(1438367432, 
1438367492, 1438367552, 1438367612, 1438367672, 1438367732), tzone = "", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(6L, 1L))

In other words, I have xts object as
2015-08-01 00:00:32 6.000011e-18
2015-08-01 00:01:32 6.000011e-18
2015-08-01 00:02:32 6.000011e-18

How should I set seconds to 0 so that it will result into
2015-08-01 00:00:00 6.000011e-18
2015-08-01 00:01:00 6.000011e-18
2015-08-01 00:02:00 6.000011e-18


If you datetime is POSIXct, I guess my solution should work

It's usually not a good idea to change timestamps to be earlier than observed, so you should carefully reconsider doing this. If you provide more information on the actual problem you're trying to solve, I can probably help you avoid doing something as potentially dangerous as changing your data to have occurred before it actually did.
align.time will (safely) round the timestamp up to the nearest n seconds:
> align.time(x, n=60)
                            [,1]
2015-07-31 13:31:00 6.000011e-18
2015-07-31 13:32:00 6.000011e-18
2015-07-31 13:33:00 6.000011e-18
2015-07-31 13:34:00 6.000011e-18
2015-07-31 13:35:00 6.000011e-18
2015-07-31 13:36:00 6.000011e-18

Or, if you must, you can just use trunc to remove the seconds all-together:
> index(x) <- as.POSIXct(trunc(index(x), units="mins"))
> x
                            [,1]
2015-07-31 13:30:00 6.000011e-18
2015-07-31 13:31:00 6.000011e-18
2015-07-31 13:32:00 6.000011e-18
2015-07-31 13:33:00 6.000011e-18
2015-07-31 13:34:00 6.000011e-18
2015-07-31 13:35:00 6.000011e-18


I have minor issues with both of these regarding the output: (1) align.time shifts time to next minute, which I do not want. I Just want to set my seconds to 0 and retain original minutes, hours, etc  (2) with trunc I get an output in the form of POSIXlt or POSIXt, hence  I need to again use as.POSIXct function. Currently, I am using the second option. But, I think there must be some option to set the seconds to 0 while retaining type (POSIXct) of time format

@HaroonRashid: I only provided the align.time solution because it's safe. Without knowing more details of the actual problem you're trying to solve, I do not recommend you change your data so that it has a timestamp before it was measured or recorded. That has the potential to cause major issues with analysis by looking into the future. I didn't realize trunc provided a POSIXlt object. It's straight-forward to wrap that call in as.POSIXct, as I've done in my edit.;

Not sure if this is the OP´s issue but I can think of one situation where one would want to ´ignore´ the seconds - where you have two temporal data sets taken on data loggers and you want to merge the data sets but because you pressed start seconds apart the time logs on one are taken every nth second and on the second are taken every yth second.  This prevents the merge on datasets where what happens between the nth and yth second may be too small a time interval to care about (at the analyser´s discression of course).  trunc is a good solution for this.

We may use sub.  We match the two digits (\\d{2}) before the end of the string ($) and replace it with 00 of the index of the 'xt2'.  As the return object is character class, we can reconvert it to POSIXct class before assigning it back as the index of the original object. 
index(xt2) <- as.POSIXct(sub('\\d{2}$', '00', index(xt2)))


Can you please explain how pattern d{2}$ refers to seconds.  I am trying to decode the syntax of this, For example, what if I want to change minutes to some xx

This will only work in the case where the index is already POSIXct and will fail if the index values have sub-seconds.

Doing string processing on dates and times is generally a very bad idea.
