1

I have a dataframe with hundreds of columns, I want to rescale those columns to be between 0 and 1 but based on the min/max of all columns.

My data looks like this:

a<-c(1, 3, 4, 6, 8.7, 9, 10, 12, 19.3, 20)
b<-c(10, 30, 40, 60, 87, 90, 100, 120, 190, 200)
df<-data.frame(a=a, b=b)
> df
      a   b
1   1.0  10
2   3.0  30
3   4.0  40
4   6.0  60
5   8.7  87
6   9.0  90
7  10.0 100
8  12.0 120
9  19.3 190
10 20.0 200

So here the min is 1 and the max is 200.

I liked this function but It only can be applied to a vector :

rescale(*df*, to = c(0, 1), from = range(*df[,1:2]*, na.rm = TRUE, finite = TRUE))

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
roger
  • 89
  • 10

2 Answers2

2

this should work

lapply(df, function(x) scale(x, center = FALSE, scale = max(x, na.rm = TRUE)/100)))

EDIT:

if your objective is to use the maximum value of any of the columns as a reference for the rescale you can do something like this:

lapply(df, function(x) scale(x, center = FALSE, scale = max(df, na.rm = TRUE)/100))

your columns would assume this values:

a
       [,1]
 [1,]  0.50
 [2,]  1.50
 [3,]  2.00
 [4,]  3.00
 [5,]  4.35
 [6,]  4.50
 [7,]  5.00
 [8,]  6.00
 [9,]  9.65
[10,] 10.00
attr(,"scaled:scale")
[1] 2

b
       [,1]
 [1,]   5.0
 [2,]  15.0
 [3,]  20.0
 [4,]  30.0
 [5,]  43.5
 [6,]  45.0
 [7,]  50.0
 [8,]  60.0
 [9,]  95.0
[10,] 100.0

is this what you want?

Carbo
  • 906
  • 5
  • 23
  • thanks but it does not work.. In fact the scale should be based on the range of all wanted column df[,1:2] (min/max) to scale them not separately – roger Feb 07 '20 at 14:48
  • I changed my answer, I hope I understood correctly what you want to obtain – Carbo Feb 07 '20 at 15:21
1
mx <- max(df)
mn <- min(df)
(df - mn) / (mx - mn)

            a          b
1  0.00000000 0.04522613
2  0.01005025 0.14572864
3  0.01507538 0.19597990
4  0.02512563 0.29648241
5  0.03869347 0.43216080
6  0.04020101 0.44723618
7  0.04522613 0.49748744
8  0.05527638 0.59798995
9  0.09195980 0.94974874
10 0.09547739 1.00000000
s_baldur
  • 29,441
  • 4
  • 36
  • 69