0

I have 9x2 dataframe DATS with prices and POSIXct datetimestamps sampled every 15 minutes. and a list of dates FOMCDATES with the dates of recent FOMC events. I then split the POSIXct datetimestamps into separate Date and Time columns. I then add column FOMCBinary to DATS containing a 1 whenever the date in DATS is contained in FOMCDATES AND time is 14:30 (EDIT: FOMC is 14:00, used 14:30 by mistake - example still valid).

I would like to record the Close before the event takes place in a separate variable. The name of the variable should be based on the date of the event. In the case at hand, the result should be: PreEvent-2016-01-27 = 1122.7. Please take into account this would actually be run in a large sample with dozens of dates and the time can be other than 14:30 (e.g. if looking at NFP rather than FOMC).

DATS <- structure(list(DateTime = structure(list(sec = c(0, 0, 0, 0,0, 0, 0, 0, 0), min = c(30L, 15L, 0L, 45L, 30L, 15L, 0L, 45L,30L), hour = c(15L, 15L, 15L, 14L, 14L, 14L, 14L, 13L, 13L),mday = c(27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L), mon = c(0L,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), year = c(116L, 116L, 116L,116L, 116L, 116L, 116L, 116L, 116L), wday = c(3L, 3L, 3L,3L, 3L, 3L, 3L, 3L, 3L), yday = c(26L, 26L, 26L, 26L, 26L,26L, 26L, 26L, 26L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,0L, 0L), zone = c("EST", "EST", "EST", "EST", "EST", "EST","EST", "EST", "EST"), gmtoff = c(NA_integer_, NA_integer_,NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,NA_integer_, NA_integer_)), .Names = c("sec", "min", "hour","mday", "mon", "year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), Close = c(1127.2, 1127.5,1126.9, 1128.3, 1125.4, 1122.7, 1122.8, 1117.3, 1116)), .Names = c("DateTime","Close"), row.names = 2131:2139, class = "data.frame") 
FOMCDATES <- structure(c(16785, 16827, 16876), class = "Date") 

DATS$Time <- strftime(DATS$DateTime, format="%H:%M:%S")             
DATS$Date <- as.Date(DATS$DateTime) 

DATS$FOMCBinary <- ifelse(  DATS$Time == "14:30:00" & DATS$Date %in% FOMCDATES, 1, 0)


#Output for FOMCDATES: 
[1] 2015-12-16 2016-01-27 2016-03-16    

#Output for DATS after calculations performed:
                DateTime  Close     Time       Date FOMCBinary
2131 2016-01-27 15:30:00 1127.2 15:30:00 2016-01-27          0
2132 2016-01-27 15:15:00 1127.5 15:15:00 2016-01-27          0
2133 2016-01-27 15:00:00 1126.9 15:00:00 2016-01-27          0
2134 2016-01-27 14:45:00 1128.3 14:45:00 2016-01-27          0
2135 2016-01-27 14:30:00 1125.4 14:30:00 2016-01-27          1
2136 2016-01-27 14:15:00 1122.7 14:15:00 2016-01-27          0
2137 2016-01-27 14:00:00 1122.8 14:00:00 2016-01-27          0
2138 2016-01-27 13:45:00 1117.3 13:45:00 2016-01-27          0
2139 2016-01-27 13:30:00 1116.0 13:30:00 2016-01-27          0

My attempt results in a vector rather than a single value, and the variable name is not dynamic.

#My failed attempt

#Define rowShift function
rowShift <- function(x, shiftLen = 1L) { 
 r <- (1L + shiftLen):(length(x) + shiftLen)
 r[r<1] <- NA
 return(x[r])  }

PreEventLevel <- ifelse(DATS$FOMCBinary > 0, rowShift(DATS$Close, +1), 0)

How could this be achieved? Thank you very much!

Krug
  • 1,003
  • 13
  • 33
  • A function I missed to post. Thanks for asking. Will edit! – Krug Apr 24 '16 at 16:24
  • Mmh is not good practice generates variables with dynamic names... can't you simply use a list as container for your variables ? – digEmAll Apr 24 '16 at 16:36
  • I can and in fact I need to, as I need to create a PreEventLevel variable that contains only the last PreEventLevel (e.g. PreEventLevel = 1122.7 until a new FOMC takes place the following month). Also failed to achieve this, but thought to ask it in a separate question. The purpose of the dynamic name is to be able to record the level for every FOMC event in a separate variable. Maybe it is not the most efficient way to achieve it, and a vector would be better better. – Krug Apr 24 '16 at 16:42

1 Answers1

1

Creating variables in the global environment with dynamic names is not a good practice... I would rather use a list as container for your values e.g. :

# get the indexes where FOMCBinary > 0
oneIdxs <- which(DATS$FOMCBinary > 0)
# get the close values using indexes on the shifted vector and put the values in a list
PreEventLevel <- as.list(rowShift(DATS$Close,1)[oneIdxs])
# set the dates as names of the element in the list
names(PreEventLevel) <- DATS$Date[oneIdxs]

> PreEventLevel  
$`2016-01-27`
[1] 1122.7

# now you can access to values using: 
# PreEventLevel[["2016-01-27"]] 
# or 
# PreEventLevel$`2016-01-27`

Note that you can also simply create a vector with names instead of a list (just remove as.list), and PreEventLevel will be:

> PreEventLevel
2016-01-27 
    1122.7
# you can access to values using PreEventLevel["2016-01-27"]
digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • @Gracos: no problem, added a note explaining that in this case list is not absolutely necessary, you can also use a named vector ;) – digEmAll Apr 24 '16 at 16:45