# [R] Truncating (rounding down) to nearest half hour.

arun smartpink111 at yahoo.com
Fri Jul 20 08:58:53 CEST 2012

```Hi,

Just a variant of creating rounded down minutes vector:

minutes<-ifelse(ceiling(as.numeric(substr(t[,1],15,16))>30),"30","00")
#or
minutes1<-ifelse(floor(as.numeric(substr(dat1[,1],15,16))>30),"30","00")
identical(minutes,minutes1)
#[1] TRUE

minutes3<-floor( as.numeric( substr( t[,1], 15, 16 ) ) / 30 ) * 30
minutes3<- ifelse( minutes == 30, "30", "00" )
identical(minutes,minutes3)
[1] TRUE

A.K.

----- Original Message -----
From: Rainer Schuermann <Rainer.Schuermann at gmx.net>
To: r-help at r-project.org; APOCooter <mikeedinger16 at gmail.com>
Cc:
Sent: Friday, July 20, 2012 1:07 AM
Subject: Re: [R] Truncating (rounding down) to nearest half hour.

My amateur approach:

I put your data in a dataframe called t:
Date Score
1 2008-05-01 08:58:00    80
2 2008-05-01 13:31:00    11
3 2008-05-01 16:35:00    81
4 2008-05-01 23:20:00   152
5 2008-05-02 01:01:00   130
6 2008-05-02 03:35:00   122

Then I created a vector with rounded down minutes:
> minutes <- floor( as.numeric( substr( t[,1], 15, 16 ) ) / 30 ) * 30
> minutes <- ifelse( minutes == 30, "30", "00" )
[1] "30" "30" "30" "00" "00" "30"

Next the replacement:
> tc <- t
> tc[,1] <- as.character( t[,1] )
> substr( tc[,1] , 15, 16 ) <- minutes
> tc[,1] <- as.POSIXct( tc[,1] )
Date Score
1 2008-05-01 08:30:00    80
2 2008-05-01 13:30:00    11
3 2008-05-01 16:30:00    81
4 2008-05-01 23:00:00   152
5 2008-05-02 01:00:00   130
6 2008-05-02 03:30:00   122

Is that what you were looking for?

Rgds,
Rainer

-------- Original-Nachricht --------
> Datum: Thu, 19 Jul 2012 10:03:23 -0700 (PDT)
> Von: APOCooter <mikeedinger16 at gmail.com>
> An: r-help at r-project.org
> Betreff: [R] Truncating (rounding down) to nearest half hour.

> I couldn't find anything in the chron or timeDate packages, and a good
> search
> yielded rounding to the nearest half hour, which I don't want.
>
> The data:
>
> structure(list(Date = structure(c(1209625080, 1209641460, 1209652500,
> 1209676800, 1209682860, 1209692100, 1209706980, 1209722580, 1209726300,
> 1209739620, 1209762780, 1209765720, 1209770520, 1209791040, 1209812580,
> 1209829920, 1209837180, 1209848160, 1209854640, 1209859440, 1209870780,
> 1209887760, 1209901080, 1209921660, 1209929280, 1209945600, 1209957240,
> 1209980280, 1210001760, 1210017000, 1210021140, 1210034820, 1210042800,
> 1210048980, 1210061520, 1210074480, 1210081200, 1210089300, 1210095960,
> 1210104120, 1210110900, 1210110900, 1210118400, 1210126980, 1210134180,
> 1210142640, 1210156080, 1210164180, 1210176840, 1210183740), class =
> c("POSIXct",
> "POSIXt"), tzone = ""), Score = c(80L, 11L, 81L, 152L, 130L,
> 122L, 142L, 20L, 1L, 31L, 93L, 136L, 128L, 112L, 48L, 57L, 92L,
> 108L, 100L, 107L, 81L, 37L, 47L, 70L, 114L, 125L, 99L, 46L, 108L,
> 106L, 111L, 75L, 75L, 136L, 36L, 13L, 35L, 71L, 105L, 113L, 116L,
> 116L, 94L, 130L, 102L, 19L, 1L, 33L, 78L, 89L)), .Names = c("Date",
> "Score"), row.names = c(NA, 50L), class = "data.frame")
>
> I'm trying to round the time down to the nearest half hour, without losing
> the date.  For example, 01/01/2009 1:29 would round to 01/01/2009 1:00,
> while 01/01/2009 1:31 would round to 01/01/2009 1:30.
>
> Any help is greately appreciated.
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Truncating-rounding-down-to-nearest-half-hour-tp4637083.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
-------

Gentoo Linux with KDE

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help