- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在相同的观察中对多个间隔的天数求和。我在 StackOverflow 中看到了许多关于此任务的不同示例。尽管如此,我还是无法使用我的数据重现它们,因为我必须在两次以上重叠的时间和多个时间间隔内进行。
library(lubridate)
library(dplyr)
a <- c(as_date(0), as_date(8), as_date(80),as_date(60))
b <-c(as_date(2), as_date(20), as_date(100),as_date(80))
c <-c(as_date(1), as_date(16), as_date(95),as_date(85))
d <- c(as_date(100), as_date(19), as_date(120),as_date(100))
e <-c(as_date(0), as_date(50), as_date(101),as_date(65))
f <- c(as_date(150), as_date(100), as_date(200),as_date(200))
df <- data.frame(int.1 = interval(a, b), int.2 = interval(c, d), int.3 = interval(e, f))
df %>%
mutate(overlapping.time = int.1 %/% days(1) + int.2 %/% days(1) + int.3 %/% days(1))
int.1 int.2 int.3 overlapping.time
1 1970-01-01 UTC--1970-01-03 UTC 1970-01-02 UTC--1970-04-11 UTC 1970-01-01 UTC--1970-05-31 UTC 251
2 1970-01-09 UTC--1970-01-21 UTC 1970-01-17 UTC--1970-01-20 UTC 1970-02-20 UTC--1970-04-11 UTC 65
3 1970-03-22 UTC--1970-04-11 UTC 1970-04-06 UTC--1970-05-01 UTC 1970-04-12 UTC--1970-07-20 UTC 144
4 1970-03-02 UTC--1970-03-22 UTC 1970-03-27 UTC--1970-04-11 UTC 1970-03-07 UTC--1970-07-20 UTC 170
最佳答案
下面是一个函数 overlapping_days()
,这将采用一组间隔列并计算重叠天数的总数。请参阅内联注释以了解其工作原理。它涵盖了完全包含在另一个中、部分重叠的区间,并且不对列之间的关系做任何假设。从您之前的计算中减去函数的结果将得到您想要的结果。请注意,我从您最初发布的内容中修改了我使用的数据。
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:lubridate':
#>
#> intersect, setdiff, union
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
a <- c(as_date(0), as_date(1), as_date(80),as_date(60))
b <-c(as_date(20), as_date(22), as_date(100),as_date(80))
c <-c(as_date(1), as_date(16), as_date(95),as_date(85))
d <- c(as_date(3), as_date(19), as_date(120),as_date(100))
e <-c(as_date(0), as_date(50), as_date(101),as_date(65))
f <- c(as_date(150), as_date(100), as_date(200),as_date(200))
df <- data.frame(int.1 = interval(a, b), int.2 = interval(c, d), int.3 = interval(e, f))
overlapping_days <- function(...) {
# Collect the vectors passed into a list
ll <- list(...)
# Create all possible 2-combinations for the number of columns passed in.
combinations <- combn(length(ll), 2)
# Create a column for each combination, and a row for each element in the vectors.
overlaps <- matrix(data = 0, nrow = length(ll[[1]]), ncol = length(combinations))
# Loop through the combinations
iterations <- seq_len(ncol(combinations))
for (k in iterations) {
# I'll refer to each of these indices as intervals -- they each represent
# a vector passed in.
i <- combinations[1, k]
j <- combinations[2, k]
overlaps[,k] <- case_when(
# If the interval i is within interval j, add i to the overlap
ll[[i]] %within% ll[[j]] ~ ll[[i]] %/% days(1),
# If the interval j is within interval i, add j to the overlap
ll[[j]] %within% ll[[i]] ~ ll[[j]] %/% days(1),
# If they overlap, either int_start(i) < int_end(j), or int_start(j) < int_end(i)
# Calculate the appropriate difference -- these look backwards but
# are needed so a positive number is produced.
int_overlaps(ll[[i]], ll[[j]]) & int_start(ll[[j]]) < int_end(ll[[i]]) ~
int_start(ll[[j]]) %--% int_end(ll[[i]]) %/% days(1),
int_overlaps(ll[[j]], ll[[i]]) & int_start(ll[[i]]) < int_end(ll[[j]]) ~
int_start(ll[[i]]) %--% int_end(ll[[j]]) %/% days(1),
# If none of these are true, the intervals do not overlap and we add 0 to
# the overlap amount.
TRUE ~ 0
)
}
# Sum across rows to get the total number of overlapping days.
rowSums(overlaps)
}
df %>%
mutate(overlapping.time = int.1 %/% days(1) + int.2 %/% days(1) + int.3 %/% days(1), overlap = overlapping_days(int.1, int.2, int.3))
#> Note: method with signature 'Timespan#Timespan' chosen for function '%/%',
#> target signature 'Interval#Period'.
#> "Interval#ANY", "ANY#Period" would also be valid
#> int.1 int.2
#> 1 1970-01-01 UTC--1970-01-21 UTC 1970-01-02 UTC--1970-01-04 UTC
#> 2 1970-01-02 UTC--1970-01-23 UTC 1970-01-17 UTC--1970-01-20 UTC
#> 3 1970-03-22 UTC--1970-04-11 UTC 1970-04-06 UTC--1970-05-01 UTC
#> 4 1970-03-02 UTC--1970-03-22 UTC 1970-03-27 UTC--1970-04-11 UTC
#> int.3 overlapping.time overlap
#> 1 1970-01-01 UTC--1970-05-31 UTC 172 24
#> 2 1970-02-20 UTC--1970-04-11 UTC 74 3
#> 3 1970-04-12 UTC--1970-07-20 UTC 144 24
#> 4 1970-03-07 UTC--1970-07-20 UTC 170 30
关于r - 将多个时间间隔相加而不计算 lubridate 中的重叠时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58945875/
我知道可以创建 object.__mul__(self, other)启用两个对象的自定义乘法的方法。然而,正如 python 文档中所解释的那样, to evaluate the expressio
我无法找到一种好方法来查找列中的所有负条目并将它们向上移动到列中,将它们与现有条目相加(即从当前条目中减去负条目),直到所有值都是正值。 重要的是,最终数据帧没有负值 & 所有以前的负条目 = 0。此
我想将 var string 中的所有元素相加。我确实尝试过这个,但现在我想对每个元素都这样做。 var operator = document.getElementById("operation")
“任何长度的正数表示为数字字符数组,因此介于‘0’和‘9’之间。我们知道最重要的密码位于数组索引 0 的位置。 例子: - 号码是 10282 - 数组将是数字 = [1,0,2,8,2] 考虑到这一
这是我的查询: UPDATE qanda SET amount = amount + 1000 WHERE id = ? AND type = 0; 默认情况下, amount 列为 null,当我执
不同列的值为1时,每列加10分。 要求将累加的结果汇总到表格的一列中。但总分不得超过20分。因此,如果总和达到 30,列中的结果将始终为 20。 SELECT CASE WHE
public static void main(String[] args) { Vector vec = new Vector(); vec.add(new
我正在尝试使用 js 或 jquery 将 +20,2 添加到带有点 9990.95 的总和或带有逗号 9990,95 的总和 9990.95 var price = $( '.sum' ).text
我如何编写一个函数来将二维数组中每一行的内容相加?要添加每一列的内容?我的代码(到目前为止): #include using namespace std; const int QUARTER = 4
这个问题在这里已经有了答案: Count the number of set bits in a 32-bit integer (65 个答案) 关闭 8 年前。 我有这段代码可以获取数字的二进制表
创建了一个 Div,在其中我有 label 元素和 input 元素,我想在每个 div 中获得不同的标签值。如何重用我的 div 组件而不是再次编写相同的代码。 我尝试在 Stackoverflow
所以我在构建代码时遇到了这个问题。这个问题 This work is based on operator overloading, you need to build a string calcula
为了好玩并进一步了解 float 的工作原理,我尝试制作一个函数,它接受两个单精度 float ,并将它们相加。 到目前为止,我所做的对于相同符号的数字非常有效,但当数字具有相反的符号时,它就会分崩离
我想添加两个 map 以及以下行为。 如果键存在 -> 将两个键值相加。 如果键不存在 -> 插入对映射。 我看过一些标准库算法。即转换,但似乎没有做我想要的。 取自此LINK template
我的程序中有两个整数;我们称它们为“a”和“b”。我想将它们加在一起并得到另一个整数。这些是常规的 Python int 对象。我在想;我如何将它们与 Twisted 一起添加?某处是否有特殊的 pe
因此,我必须创建这个程序,从文件中读取多个“工资”,然后将所有工资相加以返回一个“总工资”,并计算所有工资的平均值。 我目前拥有的代码如下: package uploadTask7_countingS
我的结果是这样的: +-------+--------+--------+-----+--------------+ | Count | Equipe | IdTeam | Id | Nam
我有以下数据框: df2 = pd.DataFrame({'season':[1,1,1,2,2,2,3,3],'value' : [-2, 3,1,5,8,6,7,5], 'avail':[3,3,
所以我正在过滤数据(所有字符串),并希望显示以下内容: 第一个字母包含您的搜索的搜索词(例如,在搜索 'Br' 时,我想查看所有以 'Br' 开头的词,例如 Break , 坏了,...) 字符串中包
我试图通过以下关系(仅显示父级)来说明一对多关系的总和: @Entity @Table(name = "Parent") public class Parent implements Serializ
我是一名优秀的程序员,十分优秀!