7

I normalized data with the minimum and maximum with this R code:

normalize <- function(x) {
    return ((x - min(x)) / (max(x) - min(x)))
  }

mydata <- as.data.frame(lapply(mydata , normalize))

How can I denormalize the data ?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
myID33
  • 159
  • 1
  • 2
  • 7
  • 5
    So what's the desired output here? Why don't you just save the "normalized" version to a different variable. There is no way to uniquely unnormalize data with this formula. `c(0,10)` would be normalized to `c(0,1)` but so would `c(3, 17)`. It's impossible to say what the original values were. – MrFlick Sep 06 '16 at 18:54
  • Original data for example is [ (3,8,10,11,22,28), (4,17,20,21,26,40), (4,5,13,16,18,27)] – myID33 Sep 06 '16 at 18:57
  • 2
    You should edit your post to include important info (like the example data) rather than leaving it in a comment. – Frank Sep 06 '16 at 19:19

1 Answers1

16

Essentially, you just have to reverse the arithmetic: x1 = (x0-min)/(max-min) implies that x0 = x1*(max-min) + min. However, if you're overwriting your data, you'd better have stored the min and max values before you normalized, otherwise (as pointed out by @MrFlick in the comments) you're doomed.

Set up data:

dd <- data.frame(x=1:5,y=6:10)

Normalize:

normalize <- function(x) {
    return ((x - min(x)) / (max(x) - min(x)))
}
ddnorm <- as.data.frame(lapply(dd,normalize))
##      x    y
## 1 0.00 0.00
## 2 0.25 0.25
## 3 0.50 0.50
## 4 0.75 0.75
## 5 1.00 1.00

Denormalize:

minvec <- sapply(dd,min)
maxvec <- sapply(dd,max)
denormalize <- function(x,minval,maxval) {
    x*(maxval-minval) + minval
}
as.data.frame(Map(denormalize,ddnorm,minvec,maxvec))
##   x  y
## 1 1  6
## 2 2  7
## 3 3  8
## 4 4  9
## 5 5 10

A cleverer normalize function would attach the scaling variables to the result as attributes (see the ?scale function ...)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453