0

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?

asaha
  • 53
  • 5
  • Not clear your requirement, if col1 should be the weight, what should be the 'n' value for runif? Can you share your expected output. – Karthik S Nov 22 '20 at 12:55
  • Hi Karthik, i have used runif but that's generating random numbers regardless of the magnitude of col1.I am expecting if col1=16 it should be 100,col1=0 then it should be 0. – asaha Nov 22 '20 at 13:00

4 Answers4

1

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
Karthik S
  • 11,348
  • 2
  • 11
  • 25
1

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
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

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
AlexB
  • 3,061
  • 2
  • 17
  • 19
0

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
akrun
  • 874,273
  • 37
  • 540
  • 662