Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i rescale every column in my data frame to a 0-100 scale? (in r)

Tags:

r

scaling

i am trying to get all the colums of my data frame to be in the same scale..

right now i have something like this... where a is on a 0-1 scale b is on a 100 scale and c is on a 1-5 scale

a   b     c 
0   89   4 
1   93   3 
0   88   5

How would i get it to a 100scale like this...

a     b      c 
0     89     80 
100   93     60 
0     88     100 

i hope that is somewhat clear.. i have tried scale() but can not seem to get it to work.

like image 745
Matt.G Avatar asked Oct 19 '13 04:10

Matt.G


People also ask

How do I rescale data in R?

In R, you can use the scale() function to scale the values in a vector, matrix, or data frame. You will almost always receive meaningless results if you do not normalize the vectors or columns you are utilizing. Scale() is a built-in R function that centers and/or scales the columns of a numeric matrix by default.

How do I standardize a column in R?

Method 1: Using Scale function. R has a built-in function called scale() for the purpose of standardization. Here, “x” represents the data column/dataset on which you want to apply standardization. “center” parameter takes boolean values, it will subtract the mean from the observation value when it is set to True.


2 Answers

Using scale, if dat is the name of your data frame:

## for one column
dat$a <- scale(dat$a, center = FALSE, scale = max(dat$a, na.rm = TRUE)/100)
## for every column of your data frame
dat <- data.frame(lapply(dat, function(x) scale(x, center = FALSE, scale = max(x, na.rm = TRUE)/100)))

For a simple case like this, you could also write your own function.

fn <- function(x) x * 100/max(x, na.rm = TRUE)
fn(c(0,1,0))
# [1]   0 100   0
## to one column
dat$a <- fn(dat$a)
## to all columns of your data frame
dat <- data.frame(lapply(dat, fn))
like image 161
Blue Magister Avatar answered Sep 25 '22 04:09

Blue Magister


My experience is that this is still unanswered, what if one of the columns had a -2, the current answer would not produce a 0-100 scale. While I appreciate the answer, when I attempted it, I have variables that are -100 to 100 and this left some negative still?

I have a solution in case this applies to you:

rescale <- function(x) (x-min(x))/(max(x) - min(x)) * 100
dat <- rescale(dat)
like image 28
J Walt Avatar answered Sep 23 '22 04:09

J Walt