12

I have data in this form:

        ds           y
1   2015-12-31 51737806366
2   2016-01-01   451800500
3   2016-01-04    48503189
4   2016-01-06      221000
5   2016-01-07   542483038
6   2016-01-08   628189789
7   2016-01-09   556762005
8   2016-01-10   195672447
9   2016-01-11   279202668
10  2016-01-12   540234196
11  2016-01-13  3403591404
12  2016-01-14   610409176

the values in the y column represent income, money units. I made an exploratory plot of this data in its original form and found the plot not too useful, the visual of the data was not appropriate, so in order to improve my visualizations I applied a log2() transformation to the y column... it worked fine:

        ds        y
1   2015-12-31 35.59050
2   2016-01-01 28.75111
3   2016-01-04 25.53158
4   2016-01-06 17.75369
5   2016-01-07 29.01500
6   2016-01-08 29.22663
7   2016-01-09 29.05249
8   2016-01-10 27.54387
9   2016-01-11 28.05674
10  2016-01-12 29.00901
11  2016-01-13 31.66441 

The problem now is that in order to complete my analysis I need to get the y values back to their original form. Is there any way to reverse the log2() transformation I applied so I can get the original numbers back?

2
  • 3
    To raise a number to a power in R, you use x ^ z. Here, to convert that column you would do 2 ^ my_data$y or similar.
    – Russ Hyde
    Jun 10, 2018 at 20:36
  • The main problem with this is that you're changing your data instead of creating new variables (columns). Look into mutate of tidyverse on how to do that easily.
    – liborm
    Jun 10, 2018 at 21:14

1 Answer 1

20

It's simple.

First, call log2:

data$y = log2(data$y)

After that, if you want to have the original y back just do:

data$y = 2^data$y

The logarithm is the inverse function to exponentiation.

The general rule is:

   logb(x) = y as by = x   

For instance:

   log2(4) = 2 as 22 = 4
   log2(8) = 3 as 23 = 8 

2
  • Thiago, your help is very much appreciated, you really made me to perfectly understand the concept. it worked just fine!! Thanks a lot!! :D Jun 11, 2018 at 13:57
  • 1
    I'm happy that you got it. Fantastic! Jun 11, 2018 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.