d-max
d-max

Reputation: 177

Repalce 1 by random values in R

prob=structure(list(x1 = c(0.04, 0.05, 0.1, 0.01, NA, NA, NA, NA, 
NA, NA, NA), x2 = c(0.08, 0.04, 0.05, 0.05, NA, NA, NA, NA, NA, 
NA, NA), x3 = c(0.08, 0.04, 0.09, 0.04, NA, NA, NA, NA, NA, NA, 
NA), x4 = c(0.03, 0.1, 0.08, 0.07, NA, NA, NA, NA, NA, NA, NA
), x5 = c(7.32, 0.05, 0.04, 1.64, NA, NA, NA, NA, NA, NA, NA)), .Names = c("x1", 
"x2", "x3", "x4", "x5"), class = "data.frame", row.names = c(NA, 
-11L))

How in x5 column ,if value>1 then replace with random values from 0.55 to 0.9?

output

     x1   x2   x3   x4   x5
1  0.01 0.06 0.06 0.02 **0.60**
2  0.10 0.05 0.08 0.03 0.01
3  0.03 0.02 0.02 0.04 0.02
4  0.07 0.08 0.10 0.03 **0.89**

Upvotes: 1

Views: 33

Answers (1)

akrun
akrun

Reputation: 886948

We can use

 library(dplyr)
 prob %>%
       mutate(x5 = replace(x5, !is.na(x5), 
        sample(seq(0.55, 0.9, by = 0.1), sum(!is.na(x5)))

Upvotes: 1

Related Questions