how to rescale a column based on weight of another column in R?

307 views Asked by At

I have a dataframe which includes col1(Counts) and col2(Date)

df<-data.frame(col1=c(1,2,3,4,16,0),col2=c('10-12-2019','11-12-2019','13-01-2020','14-02-2020','01-03-2020','01-04-2020'))

I want to create another column range(0-100) based on col1 for unique dates, but when I do that it's giving me random numbers instead of considering the weight of col1

df$col3<-runif(df$col1,min=0,max=100)

how to do it?

4

There are 4 answers

0
Karthik S On BEST ANSWER

Does this work, this works on a constant change linearly for each unit increase in col1.

library(dplyr)
df %>% mutate(col3 = col1 * (100/max(col1)))
  col1       col2   col3
1    1 10-12-2019   6.25
2    2 11-12-2019  12.50
3    3 13-01-2020  18.75
4    4 14-02-2020  25.00
5   16 01-03-2020 100.00
6    0 01-04-2020   0.00
0
AlexB On

An alternative would be to use rescale function from scales package:

scales::rescale(df$col1) -> df$col3

# c  ol1       col2   col3
# 1    1 10-12-2019 0.0625
# 2    2 11-12-2019 0.1250
# 3    3 13-01-2020 0.1875
# 4    4 14-02-2020 0.2500
# 5   16 01-03-2020 1.0000
# 6    0 01-04-2020 0.0000
0
Ronak Shah On

Maybe you are trying to scale number between 0-1. You can try this function.

scale_0_to_1 <- function(x) (x-min(x, na.rm = TRUE))/
                            (max(x, na.rm = TRUE)-min(x, na.rm = TRUE))

df$col3 <- scale_0_to_1(df$col1)

df
#  col1       col2   col3
#1    1 10-12-2019 0.0625
#2    2 11-12-2019 0.1250
#3    3 13-01-2020 0.1875
#4    4 14-02-2020 0.2500
#5   16 01-03-2020 1.0000
#6    0 01-04-2020 0.0000
0
akrun On

We can create the function with range

scale_0_to_1 <- function(x) (x- min(x))/diff(range(x))
df$col3 <- scale_0_to_1(df$col1)
df$col3
#[1] 0.0625 0.1250 0.1875 0.2500 1.0000 0.0000