Home > OS >  How random values considering the smallest and largest value of a specific column
How random values considering the smallest and largest value of a specific column

Time:07-22

How do I generate random values considering the smallest and largest value of column AB to insert into column CB. So it can be any value in the range from the lowest value to the highest value.

df<-structure(list(AB = c(23,252.23,111,2345.2,123.4), CB = c("", "", "", "", "")), row.names = c(NA, 5L), class = "data.frame")

> df
       AB CB
1   23.00   
2  252.23   
3  111.00   
4 2345.20   
5  123.40 

CodePudding user response:

We may get the range of 'AB' and sample with replace = TRUE to fill the 'CB' column

df$CB <- sample(seq(min(df$AB), max(df$AB), by = 0.01), 
     length(df$AB), replace = TRUE)
  •  Tags:  
  • r
  • Related