randTime <- function(DF) {
# Double each value. This way, we can sample integers and then divide by two
# to get "on the half-hour" values.
newDF <- DF * 2
# Next, figure out how many possible half hours choices there are. That should
# be equal to EndTime - StartTime - 4 where the last 4 is for the hour buffer
# on either side. Store this in a vector
validTimes <- newDF$EndTime - newDF$StartTime - 4
# Now pick a random number from 1 to validTimes for each entry
randHalf <- vapply(validTimes, sample, integer(1L), size = 1L)
(randHalf + newDF$StartTime) / 2
}
2条答案
按热度按时间qlvxas9a1#
如果我们做两个简化的假设--第一,时间是24小时格式的整数,第二,没有一行跨越到第二天--那么下面的假设可能成立。
字符串
使用您的初始数据集,我们得到:
型
将输出格式化为时间或将输入从字符转换的额外步骤应该不会太困难。
g6ll5ycj2#
使用accepted answer from this post:
字符串
说明:
sample() +seq()
以半小时间隔随机采样