Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rounding a time

    Hello,

    I have data in the following format. I want to examine when the most activity occurs by half hours. This code extracts the hour - gen double myhour = hh(testvar). Would I need to transform the data into a string and rename time units as 1pm to 1.30 pm etc, or is there a more efficient way to do this.

    Any assistance is appreciated.

    Bob

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double testvar
         -1893406560000
         -1.8933828e+12
         -1893382140000
    -1893379380000.0002
         -1.8933705e+12
         -1893384660000
    -1893405239999.9998
         -1893401880000
         -1893401160000
         -1893401640000
         -1893393780000
    end
    format %tchh:MM testvar

  • #2
    Thanks for the clear example.

    (It seems that I have to request such an example in most of the threads I have recently posted in.)

    My preference is to round down and tag each period by when it begins. In this case there are 30 * 60 * 1000 ms in a half hour.

    Code:
    . l
    
         +---------+
         | testvar |
         |---------|
      1. |    1:44 |
      2. |    8:20 |
      3. |    8:31 |
      4. |    9:16 |
      5. |   11:45 |
         |---------|
      6. |    7:49 |
      7. |    2:06 |
      8. |    3:02 |
      9. |    3:14 |
     10. |    3:06 |
         |---------|
     11. |    5:17 |
         +---------+
    
    . gen double testvar2 = 30 * 60000 * floor(testvar / (30 * 60 * 1000))
    
    . format %tchh:MM testvar2
    
    . l
    
         +--------------------+
         | testvar   testvar2 |
         |--------------------|
      1. |    1:44       1:30 |
      2. |    8:20       8:00 |
      3. |    8:31       8:30 |
      4. |    9:16       9:00 |
      5. |   11:45      11:30 |
         |--------------------|
      6. |    7:49       7:30 |
      7. |    2:06       2:00 |
      8. |    3:02       3:00 |
      9. |    3:14       3:00 |
     10. |    3:06       3:00 |
         |--------------------|
     11. |    5:17       5:00 |
         +--------------------+
    I see precisely no reason to use a string here. A numeric variable ensures proper ordering, among other things.

    See also http://www.stata-journal.com/sjpdf.h...iclenum=dm0002

    The same problem arose in different form earlier today: http://www.statalist.org/forums/foru...-in-panel-data
    Last edited by Nick Cox; 18 Jan 2016, 13:21.

    Comment


    • #3
      Nick,
      Many thanks for the prompt reply. I had looked at round, ceil and floor but can now see why they didn't work the way I'd used them.
      Bob

      Comment


      • #4
        Despite its wonderful name, round() is a function I usually avoid, except for rounding to the nearest integer, because otherwise I can never remember exactly what it does, especially at boundary cases, where very often there is a strong idea of what should happen. (Experiment! is a fair answer to that, however.)

        floor() and ceil() are in contrast much easier to memorise and extend. For example, ceil(10 * runiform()) is an easy way to get a uniform sample from the integers 1 to 10. (There are other ways to do that too.)

        Comment

        Working...
        X