Search code examples
rdataframerescale

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


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?


Solution

  • 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