I am trying to create a new column which is formed by taking the value from column A as the upper bound of the sample which I would like to take. Any suggestions. I tried the following but no avail. Any suggestions? Suppose the value A is 15.25, then I would like to generate a value which is between 1 and 15.25 which are spaced by 1/4.
my_data = data.frame(A = sample(seq(1, 20, 1/4), 10))
my_data %>% mutate(B = sample(seq(1, A, 1/4)), 1))
CodePudding user response:
You can use rowwise() that
allows you to compute on a data frame a row-at-a-time
as follows. Other wise it would take the whole A
column into sample where you need to specify only 1 value as upper-bound:
my_data %>%
rowwise() %>%
mutate(B = sample(seq(1, A, 1/4), size = 1))
# A tibble: 10 × 2
# Rowwise:
A B
<dbl> <dbl>
1 19 9.25
2 8 6.25
3 7.25 1.5
4 3.25 2.75
5 2.75 2.25
6 11 4
7 14.8 13
8 9.25 7.75
9 10 9
10 10.5 8.25