2016-08-17 34 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))