r - How to assign day of year values starting from an arbitary date and take care of missing values?
问题描述
I have an R dataframe df_demand
with a date column (depdate)
and a dependent variable column bookings
. The duration is 365 days starting from 2017-11-02 and ending at 2018-11-01, sorted in ascending order.
We have booking data for only 279 days in the year.
dplyr::arrange(df_demand, depdate)
depdate bookings
1 2017-11-02 43
2 2017-11-03 27
3 2017-11-05 27
4 2017-11-06 22
5 2017-11-07 39
6 2017-11-08 48
.
.
279 2018-11-01 60
I want to introduce another column day_of_year in the following way:
depdate day_of_year bookings
1 2017-11-02 1 43
2 2017-11-03 2 27
3 2017-11-04 3 NA
4 2017-11-05 4 27
.
.
.
365 2018-11-01 365 60
I am trying to find the best possible way to do this.
In Python, I could use something like :
df_demand['day_of_year'] = df_demand['depdate'].sub(df_demand['depdate'].iat[0]).dt.days + 1
I wanted to know about an R equivalent
of the same.
When I run
typeof(df_demand_2$depdate)
the output is
"double"
Am I missing something?
解决方案
complete
您可以使用包中的函数为每个日期创建一行tidyr
。
首先,我正在创建一个包含一些示例数据的数据框:
df <- data.frame(
depdate = as.Date(c('2017-11-02', '2017-11-03', '2017-11-05')),
bookings = c(43, 27, 27)
)
接下来,我正在执行两个操作。首先,使用tidyr::complete
,我在我的分析中指定我想要的所有日期。我可以使用 来做到这一点seq.Date
,创建从第一天到最后一天的序列。
一旦完成,day_of_year
列就等于行号。
df_complete <- tidyr::complete(df,
depdate = seq.Date(from = min(df$depdate), to = max(df$depdate), by = 1)
)
df_complete$day_of_year <- 1:nrow(df_complete)
> df_complete
#> # A tibble: 4 x 3
#> depdate bookings day_of_year
#> <date> <dbl> <int>
#> 1 2017-11-02 43 1
#> 2 2017-11-03 27 2
#> 3 2017-11-04 NA 3
#> 4 2017-11-05 27 4
管道运算符的等效解决方案来自dplyr
:
df %>%
complete(depdate = seq.Date(from = min(df$depdate), to = max(df$depdate), by = 1)) %>%
mutate(days_of_year = row_number())
推荐阅读
- zsh - zsh 别名在执行时删除第一个字符
- python - 需要帮助计算神经网络反向传播中的增量项
- python - 如何将 `lambda: pass` 作为关键字参数传递?
- android - 如何在 Kotlin 测试中的 TextView 中断言多行文本
- r - 样式分组类别
- nestjs - 如何使用不同的配置多次初始化nestjs模块?
- svg - 我不明白 svglib 中的缩放比例
- c# - 如何获取 ServiceBusClient 的连接字符串?
- conv-neural-network - 为什么 Netron 将 BatchNorm2d 层渲染为我模型的偏差?
- javascript - 使用任何库的 CDN 是否比在我们的本地系统或服务器中下载更合适?