【发布时间】:2020-07-25 03:16:48
【问题描述】:
我希望在给定事件数量和感兴趣期间的天数时找到均匀间隔事件的日期。这似乎是一个微不足道的目标,但它让我感到困惑。
这是一个非常简单的示例,它有一个直接的解决方案:
n.trips <- 5
n.days <- 20
mean.trips.per.day <- n.trips / n.days
cummulative.trips <- mean.trips.per.day * c(1:n.days)
cummulative.trips
#[1] 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00
# 2.25 2.50 2.75 3.00 3.25 3.50 3.75 4.00 4.25 4.50 4.75 5.00
# Find the date of each trip
which(cummulative.trips %in% c(1:n.days))
#[1] 4 8 12 16 20
但是下面的例子不是直截了当的。显示了三种可能的解决方案,但没有一个与所需结果匹配。在这个例子中,我试图找出向量cummulative.trips 中与整数1:6 最接近的六个元素的位置。这些位置显示在矢量desired.dates:
n.trips <- 6
n.days <- 17
# Here are the desired results
date.of.first.trip <- 3 # 1.0588235
date.of.second.trip <- 6 # 2.1176471
date.of.third.trip <- 8 # or 9: 2.8235294 3.1764706; 8 is the first
date.of.fourth.trip <- 11 # 3.8823529
date.of.fifth.trip <- 14 # 4.9411765
date.of.sixth.trip <- 17 # 6.0000000
desired.dates <- c(3,6,8,11,14,17)
mean.trips.per.day <- n.trips / n.days
cummulative.trips <- mean.trips.per.day * c(1:n.days)
cummulative.trips
#[1] 0.3529412 0.7058824 1.0588235 1.4117647 1.7647059
# 2.1176471 2.4705882 2.8235294 3.1764706 3.5294118
# 3.8823529 4.2352941 4.5882353 4.9411765 5.2941176 5.6470588 6.0000000
我尝试了以下三种可能的解决方案:
# Find the date of each trip
which(cummulative.trips %in% c(1:n.days))
#[1] 17
which(round(cummulative.trips) %in% c(1:n.days))
#[1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
round(seq(1, n.days, length = n.trips))
#[1] 1 4 7 11 14 17
编辑
我尝试了 MrFlick 在评论中建议的这个函数,但它返回的结果与我在上面为我的第二个示例尝试的三种方法中的第一种方法的结果基本匹配。
What is the fastest way to check if a number is a positive natural number? (in R)
is.naturalnumber <-
function(x, tol = .Machine$double.eps^0.5) x > tol & abs(x - round(x)) < tol
x <- cummulative.trips
is.naturalnumber(x)
#[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
【问题讨论】:
-
所以你基本上只是想在列表中找到整数值?这得到 very tricky with floating point numbers 。也许使用this function to check for integers。但请记住,在涉及小数时检查确切的值时要小心。计算机的计算方式与人类不同。
-
@MrFlick 我尝试了我认为您建议的功能,但它似乎不起作用。我将它添加到我原来的帖子的底部。
-
@MrFlick 为了澄清,在我的第二个示例中,我试图挑选出向量
cummulative.trips的六个元素的位置,它们与整数 1:6 最接近。这些位置显示在矢量desired.dates中。 -
好的。这与它看起来的样子不同。看起来您正在尝试查找完全匹配,而不是最接近的匹配。