Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a new column which is less than the previous column value

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))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading