在R中将日期舍入到下一个工作日

时间:2019-04-26 15:29:34

标签: r date lubridate bizdays

我目前正在R中进行一些日期转换。我有一个带有日期列的大型财务数据集。由于证券不在周末交易,因此我的数据集中只需要有工作日。如何将此列中的日期四舍五入到上一个工作日?因此,每个星期六和星期日应转换为上一个星期五。在下面的摘录中,第一个日期是星期六,第二个日期是星期日。现在,我想将它们转换为2007-03-02,并保持其他行不变。

# A tibble: 6 x 5
Ticker Date       mean_PX_ASK mean_PX_BID Agency 
<chr>    <date>           <dbl>       <dbl> <chr>  
1 ABNANV 2007-03-03       102.        102.  Moody's
2 ABNANV 2007-03-04       102.        102.  Moody's
3 ABNANV 2007-03-12       102.        102.  Moody's
4 ABNANV 2007-03-12       102.        102.  Moody's
5 ABNANV 2008-09-17        88.9        88.4 Fitch  
6 ABNANV 2008-09-17        88.9        88.4 Fitch  

很高兴提供任何帮助!

4 个答案:

答案 0 :(得分:3)

一个简单的解决方案是使用case_when中的dplyr来检查当天的weekday是“星期六”还是“星期日”,并相应地减去天数。

library(dplyr)

df %>%
  mutate(Day = weekdays(Date), 
         Date = case_when(Day == "Saturday" ~ Date - 1, 
                          Day == "Sunday" ~ Date - 2, 
                          TRUE ~ Date)) %>%
   select(-Day)


#  Ticker       Date mean_PX_ASK mean_PX_BID  Agency
#1 ABNANV 2007-03-02       102.0       102.0 Moody's
#2 ABNANV 2007-03-02       102.0       102.0 Moody's
#3 ABNANV 2007-03-12       102.0       102.0 Moody's
#4 ABNANV 2007-03-12       102.0       102.0 Moody's
#5 ABNANV 2008-09-17        88.9        88.4   Fitch
#6 ABNANV 2008-09-17        88.9        88.4   Fitch

对于bizdays,我们需要使用create.calendar和默认weekdays创建日历。然后,我们可以使用adjust.previous获取上一个工作日。

library(bizdays)
cal <- create.calendar("Actual", weekdays=c("saturday", "sunday"))
adjust.previous(df$Date, cal)

#[1] "2007-03-02" "2007-03-02" "2007-03-12" "2007-03-12" "2008-09-17" "2008-09-17"

答案 1 :(得分:3)

可以在没有任何包的情况下在一行中完成;如果我们使用命名矢量,则可以ifelse完成

df$Date <- with(df,  Date - setNames(rep(0:2, c(5, 1, 1)), 1:7)[format(Date, "%u")])
df
#  Ticker       Date mean_PX_ASK mean_PX_BID  Agency
#1 ABNANV 2007-03-02       102.0       102.0 Moody's
#2 ABNANV 2007-03-02       102.0       102.0 Moody's
#3 ABNANV 2007-03-12       102.0       102.0 Moody's
#4 ABNANV 2007-03-12       102.0       102.0 Moody's
#5 ABNANV 2008-09-17        88.9        88.4   Fitch
#6 ABNANV 2008-09-17        88.9        88.4   Fitch

基准

使用更大的数据集

df1 <- df[rep(seq_len(nrow(df)), 1e7), ]

system.time({
df1 %>%
  mutate(Day = weekdays(Date), 
         Date = case_when(Day == "Saturday" ~ Date - 1, 
                          Day == "Sunday" ~ Date - 2, 
                          TRUE ~ Date)) %>%
   select(-Day)

})
# user  system elapsed 
# 41.468   6.881  49.588 
system.time({

with(df1,  Date - setNames(rep(0:2, c(5, 1, 1)), 1:7)[format(Date, "%u")])

})
# user  system elapsed 
# 27.456   2.785  30.490 

带有microbenchmark

library(microbenchmark)
microbenchmark(
   rs = df1 %>%
         mutate(Day = weekdays(Date), 
         Date = case_when(Day == "Saturday" ~ Date - 1, 
                          Day == "Sunday" ~ Date - 2, 
                          TRUE ~ Date)) %>%
   select(-Day),
ak = with(df1,  Date - setNames(rep(0:2, c(5, 1, 1)), 1:7)[format(Date, "%u")]), 
          times = 10L, unit = "relative")
#Unit: relative
# expr      min       lq     mean   median       uq      max neval cld
#   rs 1.401658 1.437164 1.446403 1.421731 1.512451 1.467511    10   b
#   ak 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000    10  a 

数据

df <- structure(list(Ticker = c("ABNANV", "ABNANV", "ABNANV", "ABNANV", 
"ABNANV", "ABNANV"), Date = structure(c(13575, 13576, 13584, 
13584, 14139, 14139), class = "Date"), mean_PX_ASK = c(102, 102, 
102, 102, 88.9, 88.9), mean_PX_BID = c(102, 102, 102, 102, 88.4, 
88.4), Agency = c("Moody's", "Moody's", "Moody's", "Moody's", 
"Fitch", "Fitch")), row.names = c("1", "2", "3", "4", "5", "6"
), class = "data.frame")

答案 2 :(得分:1)

在基数R中,可以将format.Date与格式字符串%u一起使用。

dates <- as.Date(c('2007-03-02', '2007-03-03', '2007-03-04'))
wd <- as.integer(format(dates, '%u'))
as.Date(ifelse(wd >= 6, dates + 5 - wd, dates), origin = '1970-01-01')
#[1] "2007-03-02" "2007-03-02" "2007-03-02"

答案 3 :(得分:1)

使用lubridate中的override func viewDidLoad() { super.viewDidLoad() nameField.text = party.value(forKey: "name") as? String sizeField.text = party.value(forKey: "size") as? String contactField.text = party.value(forKey: "contact") as? String locationField.text = party.value(forKey: "location") as? String }

wday

library(lubridate) # Generate some data dfdate <- seq.Date(from = as.Date("2019-04-26"), to = as.Date("2019-04-28"), by = "day") dfdate [1] "2019-04-26" "2019-04-27" "2019-04-28" 从wday = 1的星期日开始

wday