简体   繁体   中英

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

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 

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM