jlakes85
jlakes85

Reputation: 57

R: Filtering by an hour:min tibble dataframe column

I'm new to R and I'm looking to filter this tibble by the "HourMinStamp" column, however I can't seem to pass the appropriate HH:MM format to achieve success (other than 0). I'm looking to only keep the rows with the value "15:00".


> VIXData
# A tibble: 1,115,217 x 9
   ContractName TradeDate  HourMinStamp  Open  High   Low  Last NumPriceChanges Volume
   <chr>        <date>     <time>       <dbl> <dbl> <dbl> <dbl>           <dbl>  <dbl>
 1 F.US.VX      2016-02-29 17:01         2180  2180  2180  2180               1      4
 2 F.US.VX      2016-02-29 17:02         2180  2182  2180  2182               1      5
 3 F.US.VX      2016-02-29 17:03         2180  2180  2180  2180               1      2
 4 F.US.VX      2016-02-29 17:09         2180  2180  2179  2179               1     22
 5 F.US.VX      2016-02-29 17:11         2175  2175  2175  2175               1      1
 6 F.US.VX      2016-02-29 17:13         2175  2175  2175  2175               0      4
 7 F.US.VX      2016-02-29 17:21         2175  2175  2175  2175               0     10
 8 F.US.VX      2016-02-29 17:22         2175  2175  2175  2175               0      2
 9 F.US.VX      2016-02-29 17:23         2178  2178  2177  2177               2      4
10 F.US.VX      2016-02-29 17:29         2175  2175  2175  2175               1      1
# ... with 1,115,207 more rows

I can successfully select the "HourMinStamp" column with no issue:

> select(VIXData, HourMinStamp)
# A tibble: 1,115,217 x 1
   HourMinStamp
   <time>      
 1 17:01       
 2 17:02       
 3 17:03       
 4 17:09       
 5 17:11       
 6 17:13       
 7 17:21       
 8 17:22       
 9 17:23       
10 17:29       
# ... with 1,115,207 more rows

I can also filter for "00:00" with no issue:


> filter(VIXData, HourMinStamp == 0)
# A tibble: 577 x 9
   ContractName TradeDate  HourMinStamp  Open  High   Low  Last NumPriceChanges Volume
   <chr>        <date>     <time>       <dbl> <dbl> <dbl> <dbl>           <dbl>  <dbl>
 1 F.US.VX      2016-03-01 00'00"        2195  2195  2190  2190               1      5
 2 F.US.VX      2016-03-02 00'00"        1950  1950  1947  1947               1      2
 3 F.US.VX      2016-03-07 00'00"        1905  1905  1905  1905               0     14
 4 F.US.VX      2016-03-08 00'00"        1900  1900  1900  1900               1      2
 5 F.US.VX      2016-03-10 00'00"        1910  1910  1910  1910               0      1
 6 F.US.VX      2016-03-14 00'00"        1910  1910  1910  1910               1      1
 7 F.US.VX      2016-03-16 00'00"        1905  1905  1905  1905               1      2
 8 F.US.VX      2016-03-17 00'00"        1825  1825  1825  1825               0      1
 9 F.US.VX      2016-03-22 00'00"        1710  1710  1710  1710               0      9
10 F.US.VX      2016-03-23 00'00"        1700  1700  1700  1700               0      2
# ... with 567 more rows

Most of the documentation seems to deal with cases where a date is always paired with the HH:MM stamp, rather than just working with the HH:MM stamp itself. Any guidance would be greatly appreciated.

Upvotes: 1

Views: 68

Answers (1)

M.Viking
M.Viking

Reputation: 5398

Try filtering after defining your time with parse_time("17:01"), for example:

library(tidyverse)

df<-tibble(HourMinStamp = parse_time(c("17:01", "17:02", "17:03")))

# A tibble: 3 x 1
  HourMinStamp
  <time>      
1 17:01       
2 17:02       
3 17:03

filter(df, HourMinStamp==parse_time("17:01"))

Note, parse_time() is from the tidyverse readr package.

Upvotes: 1

Related Questions