在证券交易所关闭时代替价值。 (R)

时间:2016-08-17 11:19:20

标签: r substitution

假设我已经下载了一些股票市场数据。现在,我将它作为具有连续日期的日期列表:

enter image description here

现在我需要将上周五的值分配给周六和周日。每当股票市场关闭(如假期或其他什么),我需要分配以前的价值。我怎么在R?

谢谢!

1 个答案:

答案 0 :(得分:0)

合并data_frame,其中包含data_frame中日期的最小值和最长值之间的所有日期。

# data_frame with data that does not contain weekends
df_foo = data_frame(
  date = seq.Date(from = as.Date("2016-01-01"), 
                  to = as.Date("2016-06-01"), 
                  by = "day"),
  y = rnorm(length(date))
) %>% 
  filter(!chron::is.weekend(date))

df_foo %>% 
  left_join(
    # create a data_frame with all the dates in the range
    data_frame(date = seq.Date(from = min(df_foo$date), to = max(df_foo$date), by = "day")),
    # join with the date
    ., 
    by = "date"        
  ) %>% 
  # convert all the NA to zero
  mutate(y = ifelse(is.na(y), 0, y))