Announcement

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

  • Reverse a numeric variable

    Hi all,

    so i have a numeric variable with a lot of values, that are decimal:

    4,626276
    5,162374
    5,612217
    etc.

    The lowest value is 4,626276 and the highest value is 79,43243.

    Now, I want to reverse the variable so that it runs from highest to lowest (so that 79,43243 is the first value and 4,626276 the last value). I did it with the revrs command: revrs mcs_1st (mcs_1st is the name of the variable).
    But now the problem: This isn't actually reversing the variable. So the new variable is the same as the old one.

    So my question is? How can I reverse the variable without revrs? Is there a way?

    I hope you understand what I mean.

  • #2
    Depending on why you want that,

    Code:
    generate reversed = varname*(-1)
    should do.

    Comment


    • #3
      I assume your comma's would be decimal points in this part of the world, so I changed your data to that extent, then I did
      Code:
      qui su x, d
      gen double x2=r(max)-x+r(min)
      and the result:
      Code:
      . li, clean noo
      
                 x          x2  
          4.626276    79.43243  
          5.162374   78.896332  
          5.612217   78.446489  
          79.43243    4.626276
      is this what you're looking for? (where "x" is your original data as sort of shown above (please read the FAQ on how to provide data samples)\

      added in edit: crossed with #2 where the interpretation of what you want is quite different from my guess

      Comment


      • #4
        Given the description, perhaps a

        Code:
        gsort -x
        is wanted ...

        All we can say now is: tell us more. What is it that you are trying to achieve by reversing the variable?

        Comment


        • #5
          Lookling back at post #1, the revrs command (a community contributed command from SSC) tells us in the output of help revrs tells that it is designed for use with categorical variables, and that in the absence of the replace option it will create a new variable. The implication of the design for a categorical variable is that it expects the lowest value to be 1, rather than figure it out for itself.
          Code:
          . summarize x, meanonly
          
          . generate double x2=r(max)-x+r(min)
          
          . revrs x
          
          . summarize x, meanonly
          
          . generate double x4=r(max)-x+1
          
          . list, clean
          
                        x          x2       revx          x4  
            1.   4.626276    79.43243   75.80615   75.806154  
            2.   5.162374   78.896332   75.27006   75.270056  
            3.   5.612217   78.446489   74.82021   74.820213  
            4.   79.43243    4.626276          1           1

          Comment


          • #6
            As daniel klein pointed out: How to reverse appropriately depends on what you want to achieve. If you only want to change the sign of a correlation or regression coefficient, multiplying the variable by -1 sufficient and the easiest to do. However, if you want to interpret the mean of the variable the units and/or the scaling becomes important. Imagine that your variable is an item mean score of some Likert items transformed into a "percent of maximum possible" score where the lower end of the answer options have been linearly transformed to 0 and the higher end to 100 (note that in this case the score itself need not to include 0 or 100) by using
            Code:
            local minv = 0
            local maxv = 100
            gen x_rev = 100*(x - `minv'/(`maxv' - `minv'))
            with minv = the lower end value of the Likert scale and maxv = the upper end value of the Likert scale (note that these minimum and maximum values are not the empirical minimum and maximum values of your variable). In that case you should reverse the values of your variable such that it corresponds to a recoding of the Likert items (thus the value of the lower end becomes the value of the higher and and vice versa). Using the example above your then would reverse your scores by
            Code:
            gen x_rev = abs(100 - x)
            Thus, in that case you need to know not the empirical minimum and maximum of your data but the theoretical minimum and maximum of your variable.

            Comment

            Working...
            X