英文:
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()
中的错误:
! x
和 y
必须共享相同的来源。
英文:
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 = '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> <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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论