Ramakrishna S
Ramakrishna S

Reputation: 437

generating numbers based on a column

I want to create a column 'y' which has values between x and 1. For example, the first ten values of x are 0's. So y values can be between 0 and 1. If the x value is 0.5, then y values are between 0.5 and 1. Any suggestions? x and y are columns in a data frame.

library(dplyr)
x = rep(seq(0, 0.9, 0.1), 10) %>% sort
x
#>   [1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
#>  [19] 0.1 0.1 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.3 0.3 0.3 0.3 0.3 0.3
#>  [37] 0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.5
#>  [55] 0.5 0.5 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.7 0.7
#>  [73] 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8
#>  [91] 0.9 0.9 0.9 0.9 0.9 0.9 0.9 0.9 0.9 0.9

Upvotes: 0

Views: 30

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101916

You have couples of ways to make it, e.g.,

x + runif(length(x)) * (1 - x)

or

runif(length(x),x,1)

Upvotes: 0

Related Questions