0

Let's say I have downloaded some stock market data. Now, I am making it as a date list with continuous dates:

enter image description here

Now I need to assign a value of previous Friday to Sat and Sun. Whenever the stock market was off (like holidays or something), I need to assign the previous value. How do I go about it in R ?

Thanks!

cdomination
  • 605
  • 1
  • 7
  • 25
Shiva Prakash
  • 1,849
  • 4
  • 21
  • 25

1 Answers1

0

Merge with a data_frame that contains all the dates between the min and max of the dates in your 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))
tchakravarty
  • 10,736
  • 12
  • 72
  • 116