Announcement

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

  • rounding to 0.5, 1.5, 2.5, 3.5 etc

    Hello, I have a question in regard to rounding. As an example I have a data like this:
    Code:
    list z in 1/20
    
         +----------+
         |        z |
         |----------|
      1. |        0 |
      2. |        0 |
      3. |        0 |
      4. |        0 |
      5. | 11.36438 |
         |----------|
      6. |        0 |
      7. |      7.6 |
      8. |        0 |
      9. |        0 |
     10. |        0 |
         |----------|
     11. |        0 |
     12. |        0 |
     13. | 4.654795 |
     14. | 3.484931 |
     15. |        0 |
         |----------|
     16. | 6.446575 |
     17. |        0 |
     18. |        0 |
     19. | 21.06575 |
     20. | 3.353425 |
         +----------
    Suppose, I have also number like 0.12, 0.76, 1.3, 1.7 etc. Now, I would like to have all number between 0 and 1 to be rounded as 0.5 and all between 1 and 2 as 1.5 and so on. I was thinking of
    Code:
      round(z, 0.5)
    but this is not quite the same I was searching for.


  • #2
    Perhaps this is what you want.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float z
            0
            0
            0
            0
     11.36438
    -11.36438
          7.6
         -7.6
            0
            1
           -1
            0
     4.654795
     3.484931
            0
     6.446575
            0
            0
     21.06575
     3.353425
    end
    generate zz = floor(z)+.5
    list, clean noobs
    Code:
    . list, clean noobs
    
                z      zz  
                0      .5  
                0      .5  
                0      .5  
                0      .5  
         11.36438    11.5  
        -11.36438   -11.5  
              7.6     7.5  
             -7.6    -7.5  
                0      .5  
                1     1.5  
               -1     -.5  
                0      .5  
         4.654795     4.5  
         3.484931     3.5  
                0      .5  
         6.446575     6.5  
                0      .5  
                0      .5  
         21.06575    21.5  
         3.353425     3.5
    Note the presentation of sample data using dataex. Please use the dataex command to show example data. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run ssc install dataex to get it. Either way, run help dataex and read the simple instructions for using it. dataex will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use dataex.

    Added in edit: You may prefer the results the following gives for negative numbers.
    Code:
    generate zz = sign(z)*floor(abs(z))+.5*sign(z)
    Code:
                z      zz  
                0       0  
                0       0  
                0       0  
                0       0  
         11.36438    11.5  
        -11.36438   -11.5  
              7.6     7.5  
             -7.6    -7.5  
                0       0  
                1     1.5  
               -1    -1.5  
                0       0  
         4.654795     4.5  
         3.484931     3.5  
                0       0  
         6.446575     6.5  
                0       0  
                0       0  
         21.06575    21.5  
         3.353425     3.5
    Last edited by William Lisowski; 16 Dec 2018, 08:38.

    Comment


    • #3
      Also:
      Code:
      gen x = round(z*2)/2

      Comment


      • #4
        Thank you very much to you both! The code in #2 worked well!

        Comment

        Working...
        X