2016-08-17 5 views
0

私はいくつかの株式市場のデータをダウンロードしたとしましょう。今、私は連続日付と日付のリストとしてそれを作っています:株式取引所が閉鎖された日の値の代入。 (R)

enter image description here

今私は土曜と日曜に、前の金曜日の値を割り当てる必要があります株式市場が休み(休日など)していたときはいつでも、前の値を割り当てる必要があります。私はRでそれについてどうやって行くのですか?

ありがとうございます!

+1

'動物園:: na.locf()' – zx8754

+0

クール '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))