How to subset an H2O data frame by date in R

Let’s see below example:

> time = as.Date(c(‘2016-01-01 12:30:00’, ‘2016-02-02 10:10:22’, ‘2016-05-02 01:01:30’))

> time
[1] “2016-01-01” “2016-02-02” “2016-05-02”

lets convert above data frame to H2O data frame:

> df = data.frame(time, stringsAsFactors = FALSE)
> df
time
1 2016-01-01
2 2016-02-02
3 2016-05-02

d = as.h2o(df)
|==========================================| 100%
> d
time
1 1.451635e+12
2 1.454400e+12
3 1.462172e+12

[3 rows x 1 column]

Above d is H2O Frame which is confirmed as below:

> summary(d)
NULL
> summary(df)
 time 
 Min. :2016-01-01 
 1st Qu.:2016-01-17 
 Median :2016-02-02 
 Mean :2016-02-21 
 3rd Qu.:2016-03-18 
 Max. :2016-05-02

Our next step is to filter some values from above H2O data frame based on date comparison:

> d[d$time < as.h2o(as.Date(‘2016-05-02 01:01:30’)),]
|===================================| 100%
time
1 1.451635e+12
2 1.454400e+12

[2 rows x 1 column]

Above we have filtered date which is lower then 2016-05-02 01:01:30 and results are printed which we can store into another subset as needed.

Another option for the same is as below:

  # slice with a boolean mask
 mask <- air_h2o[,"Date"] > last_current_date
 temp_df <- air_h2o[mask,]
 mask_2 <- air_h2o[,"Date"] < new_end_date
 
 # multiply the mask dataframes to get the intersection
 final_mask <- mask*mask_2
 test_1 <- air_h2o[final_mask,]



Thank you!!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s