2016-08-17 33 views
0

比方说,我已经下载了一些股票市场数据。现在,我做它作为一个日期列表连续日期:将联合交易所关闭的天数替换为数值。 (R)

enter image description here

现在我需要上周五的值赋给周六和Sun.每当股票市场关闭(如假期或某事)时,我需要分配以前的值。我如何在R中去解决它?

谢谢!

+1

查看'动物园:: na.locf()' – zx8754

+0

cool'na.locf()'为我做的工作! –

回答

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))