Hello Everyone, I am trying a shift the time series in a dataframe (df) by 30 minutes . My current format looks something like this : *df$$Time 1* *201112312230* *201112312300* *201112312330* *I am trying to add an additional column of time (df$Time 2) next to Time 1 by lagging it by ? 30minutes. Something like this :* *df$Time1 **df$$Time2* *201112312230 **201112312200* *201112312300 **201112312230* *201112312330 **201112312300* *201112312330 * *Based on some of the suggestions available, I have tried this option * *require(zoo)* *df1$Time2 <- lag(df1$Time1, -1, na.pad = TRUE)* *View(df1)* *This does not however give me the desired result. I would appreciate any suggestions/advice in this regard.* *Thanks,* *Bhaskar* [[alternative HTML version deleted]]
Try following this example: mydf <- data.frame(t1=c('201112312230', '201112312330')) tmp1 <- as.POSIXct(mydf$t1, format='%Y%m%d%H%M') tmp2 <- tmp1 - 30*60 mydf$t2 <- format(tmp2, '%Y%m%d%H%M') It can be made into a single line, but I used intermediate variables tmp1 and tmp2 so that it would be easier to follow. Base R is more than adequate for this task. Please get rid of the asterisks in your next email. The just get in the way. Learn how to send plain text email, not HTML email. Please. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 8/31/16, 9:07 AM, "R-help on behalf of Bhaskar Mitra" <r-help-bounces at r-project.org on behalf of bhaskar.kolkata at gmail.com> wrote:>Hello Everyone, > >I am trying a shift the time series in a dataframe (df) by 30 minutes . My >current format looks something like this : > > > >*df$$Time 1* > > >*201112312230* > >*201112312300* > >*201112312330* > > > >*I am trying to add an additional column of time (df$Time 2) next to Time >1 by lagging it by ? 30minutes. Something like this :* > > >*df$Time1 **df$$Time2* > > >*201112312230 **201112312200* > >*201112312300 **201112312230* > >*201112312330 **201112312300* > >*201112312330 * > > > > > >*Based on some of the suggestions available, I have tried this option * > > > >*require(zoo)* > >*df1$Time2 <- lag(df1$Time1, -1, na.pad = TRUE)* > >*View(df1)* > > > >*This does not however give me the desired result. I would appreciate any >suggestions/advice in this regard.* > > >*Thanks,* > >*Bhaskar* > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Building on Don's example here is something that looks a lot like what I do every day: Sys.setenv(TZ="UTC") mydf <- data.frame(t1=c('2011-12-31-22-30', '2011-12-31-23-30')) library(lubridate) mydf$timestamp = lubridate::ymd_hm(mydf$t1) mydf$t2 = mydf$timestamp - period(minute=30) On Wed, Aug 31, 2016 at 2:44 PM, MacQueen, Don <macqueen1 at llnl.gov> wrote:> Try following this example: > > mydf <- data.frame(t1=c('201112312230', '201112312330')) > tmp1 <- as.POSIXct(mydf$t1, format='%Y%m%d%H%M') > tmp2 <- tmp1 - 30*60 > mydf$t2 <- format(tmp2, '%Y%m%d%H%M') > > It can be made into a single line, but I used intermediate variables tmp1 > and tmp2 so that it would be easier to follow. > > Base R is more than adequate for this task. > > Please get rid of the asterisks in your next email. The just get in the > way. Learn how to send plain text email, not HTML email. Please. > > > > > -- > Don MacQueen > > Lawrence Livermore National Laboratory > 7000 East Ave., L-627 > Livermore, CA 94550 > 925-423-1062 > > > > > > On 8/31/16, 9:07 AM, "R-help on behalf of Bhaskar Mitra" > <r-help-bounces at r-project.org on behalf of bhaskar.kolkata at gmail.com> > wrote: > > >Hello Everyone, > > > >I am trying a shift the time series in a dataframe (df) by 30 minutes . My > >current format looks something like this : > > > > > > > >*df$$Time 1* > > > > > >*201112312230* > > > >*201112312300* > > > >*201112312330* > > > > > > > >*I am trying to add an additional column of time (df$Time 2) next to Time > >1 by lagging it by ? 30minutes. Something like this :* > > > > > >*df$Time1 **df$$Time2* > > > > > >*201112312230 **201112312200* > > > >*201112312300 **201112312230* > > > >*201112312330 **201112312300* > > > >*201112312330 * > > > > > > > > > > > >*Based on some of the suggestions available, I have tried this option * > > > > > > > >*require(zoo)* > > > >*df1$Time2 <- lag(df1$Time1, -1, na.pad = TRUE)* > > > >*View(df1)* > > > > > > > >*This does not however give me the desired result. I would appreciate any > >suggestions/advice in this regard.* > > > > > >*Thanks,* > > > >*Bhaskar* > > > > [[alternative HTML version deleted]] > > > >______________________________________________ > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide > >http://www.R-project.org/posting-guide.html > >and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
That tmp1 - 30*60 can also be done as tmp1 - as.difftime(30, units="mins") so you don't have to remember that the internal representation of POSIXct is seconds since the start of 1970. You can choose from the following equivalent expressions. tmp1 - as.difftime(0.5, units="hours") tmp1 - as.difftime(1/2 * 1/24, units="days") tmp1 - as.difftime(30*60, units="secs") tmp1 - as.difftime(1/2 * 1/24 * 1/7, units="weeks") Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Aug 31, 2016 at 2:44 PM, MacQueen, Don <macqueen1 at llnl.gov> wrote:> Try following this example: > > mydf <- data.frame(t1=c('201112312230', '201112312330')) > tmp1 <- as.POSIXct(mydf$t1, format='%Y%m%d%H%M') > tmp2 <- tmp1 - 30*60 > mydf$t2 <- format(tmp2, '%Y%m%d%H%M') > > It can be made into a single line, but I used intermediate variables tmp1 > and tmp2 so that it would be easier to follow. > > Base R is more than adequate for this task. > > Please get rid of the asterisks in your next email. The just get in the > way. Learn how to send plain text email, not HTML email. Please. > > > > > -- > Don MacQueen > > Lawrence Livermore National Laboratory > 7000 East Ave., L-627 > Livermore, CA 94550 > 925-423-1062 > > > > > > On 8/31/16, 9:07 AM, "R-help on behalf of Bhaskar Mitra" > <r-help-bounces at r-project.org on behalf of bhaskar.kolkata at gmail.com> > wrote: > > >Hello Everyone, > > > >I am trying a shift the time series in a dataframe (df) by 30 minutes . My > >current format looks something like this : > > > > > > > >*df$$Time 1* > > > > > >*201112312230* > > > >*201112312300* > > > >*201112312330* > > > > > > > >*I am trying to add an additional column of time (df$Time 2) next to Time > >1 by lagging it by ? 30minutes. Something like this :* > > > > > >*df$Time1 **df$$Time2* > > > > > >*201112312230 **201112312200* > > > >*201112312300 **201112312230* > > > >*201112312330 **201112312300* > > > >*201112312330 * > > > > > > > > > > > >*Based on some of the suggestions available, I have tried this option * > > > > > > > >*require(zoo)* > > > >*df1$Time2 <- lag(df1$Time1, -1, na.pad = TRUE)* > > > >*View(df1)* > > > > > > > >*This does not however give me the desired result. I would appreciate any > >suggestions/advice in this regard.* > > > > > >*Thanks,* > > > >*Bhaskar* > > > > [[alternative HTML version deleted]] > > > >______________________________________________ > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide > >http://www.R-project.org/posting-guide.html > >and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]