每年的天数在一个开始日期和结束日期之间。

huangapple go评论102阅读模式
英文:

Days per year between a start and end date

问题

我在复制此处找到的代码方面一直不成功:

https://stackoverflow.com/questions/67076764/count-the-number-of-days-between-two-dates-per-year

注意:根据反馈更新:

我有的数据:

ID 开始日期 结束日期
60A 2018年5月4日 2022年1月10日
60B 2019年2月4日 2022年12月20日
60C 2015年8月22日 2020年6月20日

我想要的数据:

所以对于ID:60A

  1. | ID | 2018 | 2019 |2022
  2. |60A |242|365|9

我在自动复制中遇到了一个错误
auto_copy() 中的错误:
! xy 必须共享相同的来源。

英文:

I have been unsuccessful in replicating the code found here:

https://stackoverflow.com/questions/67076764/count-the-number-of-days-between-two-dates-per-year

Note: Updated per feedback:

Data I have:

ID Startdt Enddt
60A 5/4/2018 1/10/2022
60B 2/4/2019 12/20/2022
60C 8/22/2015 6/20/2020

Data I want:
so for ID: 60A

  1. | ID | 2018 | 2019 |2022
  2. |60A |242|365|9

I get an error in autocopy
Error in auto_copy():
! x and y must share the same src.

答案1

得分: 1

这是您提供的代码的翻译部分:

  1. df = read.table(text = 'ID Startdt Enddt
  2. 60A 5/4/2018 1/10/2022
  3. 60B 2/4/2019 12/20/2022
  4. 60C 8/22/2015 6/20/2020', header = T)
  5. library(dplyr)
  6. library(lubridate)
  7. library(purrr)
  8. df %>%
  9. mutate(
  10. across(ends_with("dt"), mdy), ## added this line to convert to date class
  11. date_int = interval(Startdt, Enddt),
  12. year = map2(year(Startdt), year(Enddt), seq)
  13. ) %>%
  14. unnest(year) %>%
  15. mutate(
  16. year_int = interval(as.Date(paste0(year, '-01-01')),
  17. as.Date(paste0(year, '-12-31'))
  18. ),
  19. year_sect = intersect(date_int, year_int),
  20. start_new = as.Date(int_start(year_sect)),
  21. end_new = as.Date(int_end(year_sect))
  22. ) %>%
  23. select(ID, start_new, end_new) %>%
  24. mutate(
  25. year = year(start_new),
  26. days = as.numeric(end_new - start_new) + 1 ## added 1 here as a correction
  27. ) %>%
  28. right_join(df, by = "ID") %>%
  29. pivot_wider(
  30. id_cols = c(ID, Startdt, Enddt),
  31. names_from = year, values_from = days,
  32. names_prefix = "year_",
  33. values_fill = list(days = 0)
  34. ) %>%
  35. mutate(days_number = rowSums(across(starts_with("year")))) ## updated this line to use `across()`
  36. # # A tibble: 3 × 12
  37. # ID Startdt Enddt year_2018 year_2019 year_2020 year_2021 year_2022 year_2015 year_2016 year_2017
  38. # <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
  39. # 1 60A 5/4/2018 1/10/2022 242 365 366 365 10 0 0 0
  40. # 2 60B 2/4/2019 12/20/2022 0 331 366 365 354 0 0 0
  41. # 3 60C 8/22/2015 6/20/2020 365 365 172 0 0 132 366 365
  42. # # ℹ 1 more variable: days_number <dbl>

希望这有助于您理解代码的翻译部分。

英文:
  1. df = read.table(text = &#39;ID Startdt Enddt
  2. 60A 5/4/2018 1/10/2022
  3. 60B 2/4/2019 12/20/2022
  4. 60C 8/22/2015 6/20/2020&#39;, header = T)
  5. library(dplyr)
  6. library(lubridate)
  7. library(purrr)
  8. df %&gt;%
  9. mutate(
  10. across(ends_with(&quot;dt&quot;), mdy), ## added this line to convert to date class
  11. date_int = interval(Startdt, Enddt),
  12. year = map2(year(Startdt), year(Enddt), seq)
  13. ) %&gt;%
  14. unnest(year) %&gt;%
  15. mutate(
  16. year_int = interval(as.Date(paste0(year, &#39;-01-01&#39;)),
  17. as.Date(paste0(year, &#39;-12-31&#39;))
  18. ),
  19. year_sect = intersect(date_int, year_int),
  20. start_new = as.Date(int_start(year_sect)),
  21. end_new = as.Date(int_end(year_sect))
  22. ) %&gt;%
  23. select(ID, start_new, end_new) %&gt;%
  24. mutate(
  25. year = year(start_new),
  26. days = as.numeric(end_new - start_new) + 1 ## added 1 here as a correction
  27. ) %&gt;%
  28. right_join(df, by = &quot;ID&quot;) %&gt;%
  29. pivot_wider(
  30. id_cols = c(ID, Startdt, Enddt),
  31. names_from = year, values_from = days,
  32. names_prefix = &quot;year_&quot;,
  33. values_fill = list(days = 0)
  34. ) %&gt;%
  35. mutate(days_number = rowSums(across(starts_with(&quot;year&quot;)))) ## updated this line to use `across()`
  36. # # A tibble: 3 &#215; 12
  37. # ID Startdt Enddt year_2018 year_2019 year_2020 year_2021 year_2022 year_2015 year_2016 year_2017
  38. # &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  39. # 1 60A 5/4/2018 1/10/2022 242 365 366 365 10 0 0 0
  40. # 2 60B 2/4/2019 12/20/2022 0 331 366 365 354 0 0 0
  41. # 3 60C 8/22/2015 6/20/2020 365 365 172 0 0 132 366 365
  42. # # ℹ 1 more variable: days_number &lt;dbl&gt;

huangapple
  • 本文由 发表于 2023年6月9日 01:00:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定