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

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

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

| ID | 2018 | 2019 |2022
|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

| ID | 2018 | 2019 |2022
|60A |242|365|9

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

答案1

得分: 1

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

df = read.table(text = 'ID  Startdt  Enddt
60A  5/4/2018  1/10/2022
60B  2/4/2019  12/20/2022
60C  8/22/2015  6/20/2020', header = T)

library(dplyr)
library(lubridate)
library(purrr)

df %>%
  mutate(
    across(ends_with("dt"), mdy), ## added this line to convert to date class
    date_int = interval(Startdt, Enddt),
    year = map2(year(Startdt), year(Enddt), seq)
  ) %>%
  unnest(year) %>%
  mutate(
    year_int = interval(as.Date(paste0(year, '-01-01')),
      as.Date(paste0(year, '-12-31'))
    ),
    year_sect = intersect(date_int, year_int),
    start_new = as.Date(int_start(year_sect)),
    end_new = as.Date(int_end(year_sect))
  ) %>%
  select(ID, start_new, end_new) %>%
  mutate(
    year = year(start_new),
    days = as.numeric(end_new - start_new) + 1 ## added 1 here as a correction
  ) %>%
  right_join(df, by = "ID") %>%
  pivot_wider(
    id_cols = c(ID, Startdt, Enddt),
    names_from = year, values_from = days,
    names_prefix = "year_",
    values_fill = list(days = 0)
  ) %>%
  mutate(days_number = rowSums(across(starts_with("year")))) ## updated this line to use `across()`
# # A tibble: 3 × 12
#   ID    Startdt   Enddt      year_2018 year_2019 year_2020 year_2021 year_2022 year_2015 year_2016 year_2017
#   <chr> <chr>     <chr>          <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
# 1 60A   5/4/2018  1/10/2022        242       365       366       365        10         0         0         0
# 2 60B   2/4/2019  12/20/2022         0       331       366       365       354         0         0         0
# 3 60C   8/22/2015 6/20/2020        365       365       172         0         0       132       366       365
# # ℹ 1 more variable: days_number <dbl>

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

英文:
df = read.table(text = &#39;ID 	Startdt 	Enddt
60A 	5/4/2018 	1/10/2022
60B 	2/4/2019 	12/20/2022
60C 	8/22/2015 	6/20/2020&#39;, header = T)

library(dplyr)
library(lubridate)
library(purrr)

df %&gt;%
  mutate(
    across(ends_with(&quot;dt&quot;), mdy), ## added this line to convert to date class
    date_int = interval(Startdt, Enddt),
         year = map2(year(Startdt), year(Enddt), seq)
  ) %&gt;%
  unnest(year) %&gt;%
  mutate(
    year_int = interval(as.Date(paste0(year, &#39;-01-01&#39;)),
      as.Date(paste0(year, &#39;-12-31&#39;))
    ),
    year_sect = intersect(date_int, year_int),
    start_new = as.Date(int_start(year_sect)),
    end_new = as.Date(int_end(year_sect))
  ) %&gt;%
  select(ID, start_new, end_new) %&gt;%
  mutate(
    year = year(start_new),
    days = as.numeric(end_new - start_new) + 1 ## added 1 here as a correction
  ) %&gt;%
  right_join(df, by = &quot;ID&quot;) %&gt;%
  pivot_wider(
    id_cols = c(ID, Startdt, Enddt),
    names_from = year, values_from = days,
    names_prefix = &quot;year_&quot;, 
    values_fill = list(days = 0)
  ) %&gt;%
  mutate(days_number = rowSums(across(starts_with(&quot;year&quot;)))) ## updated this line to use `across()`
# # A tibble: 3 &#215; 12
#   ID    Startdt   Enddt      year_2018 year_2019 year_2020 year_2021 year_2022 year_2015 year_2016 year_2017
#   &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;
# 1 60A   5/4/2018  1/10/2022        242       365       366       365        10         0         0         0
# 2 60B   2/4/2019  12/20/2022         0       331       366       365       354         0         0         0
# 3 60C   8/22/2015 6/20/2020        365       365       172         0         0       132       366       365
# # ℹ 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:

确定