gpt4 book ai didi

r - 如何将案例分配到不规则的时间间隔?

转载 作者:行者123 更新时间:2023-12-01 01:17:46 26 4
gpt4 key购买 nike

我有以下数据框

> head(elo)
date elo
1 1921-12-18 1597
2 1922-05-14 1583
3 1922-05-28 1595
4 1922-09-03 1595
5 1922-10-01 1612
6 1923-06-03 1597
[...]
> head(coaches)
surname start stop
Smith 1921-12-18 1922-05-28
White 1922-06-01 1923-06-28
Black 1923-09-13 1928-01-01

输出应如下所示:
        date    elo  coach
1 1921-12-18 1597 Smith
2 1922-05-14 1583 Smith
3 1922-05-28 1595 Smith
4 1922-09-03 1595 White
5 1922-10-01 1612 White
6 1923-06-03 1597 White
[...]

我如何分配 coaches$surnameelo行基于 startstop列?在 coaches 中的间隔数据框不重叠。

最佳答案

lst <- lapply(1:nrow(coaches),function(x) 
elo[which(elo$date >= coaches$start[x] & elo$date <= coaches$stop[x]),])
cbind(as.data.frame(do.call(rbind, lst)),
coach = rep(coaches$surname, sapply(lst, function(x) nrow(x))))
date elo coach
1 1921-12-18 1597 Smith
2 1922-05-14 1583 Smith
3 1922-05-28 1595 Smith
4 1922-09-03 1595 White
5 1922-10-01 1612 White
6 1923-06-03 1597 White

关于r - 如何将案例分配到不规则的时间间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11074286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com