Get the range and random days within that range using R

僤鯓⒐⒋嵵緔 提交于 2020-07-23 06:19:19

问题


I have a data frame like as shown below

test_df <- data.frame("subbject_id" = c(1,2,3,4,5), 
                      "date_1" = c("01/01/2003","12/31/2007","12/30/2008","01/02/2007","01/01/2007"))
test_df = test_df %>%
  mutate(date_1 = mdy(date_1), 
         previous_year = floor_date(date_1, 'year'), 
         next_year = ceiling_date(date_1, 'year') - 1, 
         days_to_previous_year = as.integer(date_1 - previous_year), 
         days_to_next_year = as.integer(next_year - date_1),
         rand_days_prev_year = sample.int(days_to_previous_year, 1),
         rand_days_next_year = sample.int(days_to_next_year, 1)) %>%
  select(-previous_year, -next_year)

Thanks to this post which helped me with the code to arrive at the part of the solution.

I would like to do two things

a) Get the range of values using days_to_prev_year and days_to_next_year. Note that days_to_prev_year has to have minus sign in front of it as shown in ouptut.

b) pick a random value within that range. Please note that if the range is [0,364], I want the random value to be between [1,364] inclusive. I don't want 0 as a random value. So, I would like to avoid 0 being chosen as a random value. Similarly, if it's [-11,21]. I don't want 0 to be chosen here as well but the rand value can be -11 or 21.

I tried the below statement but it doesn't work

range = paste0("[-",days_to_previous_year,",+",days_to_next_year,"]")
test_df$rand_days = sample.int(test_df$range, 1) # error as non-numeric

So, I tried using the below two numeric columns

test_df$rand_days_prev_year = sample.int(test_df$days_to_previous_year, 1) # this doesn't work
test_df$rand_days_next_year = sample.int(test_df$days_to_next_year, 1) # but this works

I get an error message like as shown below

Error in if (useHash) .Internal(sample2(n, size)) else .Internal(sample(n,  : 
  missing value where TRUE/FALSE needed

I expect my output to be like as shown below


回答1:


Here is one way :

library(dplyr)

test_df %>%
  mutate(range = sprintf("%d, %d", -days_to_previous_year, days_to_next_year)) %>%
  rowwise() %>%
  mutate(rand_days = {days = -days_to_previous_year:days_to_next_year;
                      days  = days[days != 0]
                      if(length(days)) sample(days, 1) else NA
                      })

#   subbject_id date_1     days_to_previous_year days_to_next_year range   rand_days
#        <dbl> <date>                     <int>             <int> <chr>       <int>
#1           1 2003-01-01                     0               364 0, 364        206
#2           2 2007-12-31                   364                 0 -364, 0      -220
#3           3 2008-12-30                   364                 1 -364, 1      -274
#4           4 2007-01-02                     1               363 -1, 363       228
#5           5 2007-01-01                     0               364 0, 364         72


来源:https://stackoverflow.com/questions/62707069/get-the-range-and-random-days-within-that-range-using-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!