## Subset rows according to a range of time

subset time series r

r plot subset of time series

subset ggplot2 time series

tidyverse subset

ggplot(subset)

filtering data by date range r

how to filter posixct in r

I have a data frame that looks like that

date_time loc_id node energy kgco2 1 2009-02-27 00:11:08 87 103 0.00000 0.00000 2 2009-02-27 01:05:05 87 103 7.00000 3.75900 3 2009-02-27 02:05:05 87 103 6.40039 3.43701 4 2009-02-27 03:05:05 87 103 4.79883 2.57697 5 2009-02-27 04:05:05 87 103 4.10156 2.20254 6 2009-02-27 05:05:05 87 103 2.59961 1.39599

Is there anyway I can subset it according to range of time, for example, 2am to 5am. I should then get a result that looks like this:

date_time loc_id node energy kgco2 3 2009-02-27 02:05:05 87 103 6.40039 3.43701 4 2009-02-27 03:05:05 87 103 4.79883 2.57697 5 2009-02-27 04:05:05 87 103 4.10156 2.20254

I'd use the `lubridate`

package and the `hour()`

function to make your life easier...

require( lubridate ) with( df , df[ hour( date_time ) >= 2 & hour( date_time ) < 5 , ] ) # date_time loc_id node energy kgco2 #3 2009-02-27 02:05:05 87 103 6.40039 3.43701 #4 2009-02-27 03:05:05 87 103 4.79883 2.57697 #5 2009-02-27 04:05:05 87 103 4.10156 2.20254

**Use Tidyverse Pipes to Subset Time Series Data in R,** Learn how to extract and plot data by a range of dates using pipes in R. Subset data using the dplyr filter() function. Use dplyr Also your data have a header (the first row represents column names) so set header = TRUE. The subset( ) function is the easiest way to select variables and observations. In the following example, we select all rows that have a value of age greater than or equal to 20 or age less then 10. In the following example, we select all rows that have a value of age greater than or equal to 20 or age less then 10.

One way to do it is to use `lubridate`

and define an interval :

library(lubridate) date1 <- as.POSIXct("2009-02-27 02:00:00") date2 <- as.POSIXct("2009-02-27 05:00:00") int <- new_interval(date1, date2) df[df$datetime %within% int,]

**Several examples in R on creating subsets of data via a POSIXct ,** Several examples in R on creating subsets of data via a POSIXct time object. Create a simple data frame for testing. df <- data.frame(POSIXtime = seq(as. Subset rows according to a range of time. Is there anyway I can subset it according to range of time, for example, 2am to 5am.

I would suggest using `xts`

package for time series analysis. It has very convenient subsetting functions.

DF ## date_time loc_id node energy kgco2 ## 1 2009-02-27 00:11:08 87 103 0.00000 0.00000 ## 2 2009-02-27 01:05:05 87 103 7.00000 3.75900 ## 3 2009-02-27 02:05:05 87 103 6.40039 3.43701 ## 4 2009-02-27 03:05:05 87 103 4.79883 2.57697 ## 5 2009-02-27 04:05:05 87 103 4.10156 2.20254 ## 6 2009-02-27 05:05:05 87 103 2.59961 1.39599 require(xts) XTSDATA <- xts(DF[, -1], DF[, 1]) XTSDATA["T02:00:00/T05:00:00"] ## loc_id node energy kgco2 ## 2009-02-27 02:05:05 87 103 6.40039 3.43701 ## 2009-02-27 03:05:05 87 103 4.79883 2.57697 ## 2009-02-27 04:05:05 87 103 4.10156 2.20254

**Subset a data frame based on date,** Selecting date/times in R format can be intimidating for new users. This function can be used to select quite complex dates simply - see examples below. Dates are One of the primary motivations, and key points of differentiation of the time series class xts, is the ability to subset rows by specifying ISO-8601 compatible range strings. This allows for natural range-based time queries without requiring prior knowledge of the underlying time object used in construction.

**Time Series 03: Cleaning & Subsetting Time Series Data in R ,** Export data.frame to .CSV. We can export this subset in .csv format to use in other analyses or to share with colleagues using write It’s possible to select either n random rows with the function sample_n() or a random fraction of rows with sample_frac(). We first use the function set.seed() to initiate random number generator engine.

**Time Series 04: Subset and Manipulate Time Series Data with dplyr ,** We will use pipes to efficiently perform multiple tasks within a single chunk of code. This subset was created in the Subsetting Time Series Data tutorial. filter() & slice() : filter rows based on values in specified columns; group-by() : group all Subset selection is simply selecting particular rows and columns of data from a DataFrame (or Series). This could mean selecting all the rows and some of the columns, some of the rows and all of the columns, or some of each of the rows and columns.

**How to select a range of dates in R?,** Assuming you are using the Date class: if you are using a data.frame : myData[myData$myDate >= "1970-01-01" & myData$myDate <= "2016-06-27",]. The subset() function takes 3 arguments: the data frame you want subsetted, the rows corresponding to the condition by which you want it subsetted, and the columns you want returned. In our case, we take a subset of education where “Region” is equal to 2 and then we select the “State,” “Minor.Population,” and “Education.Expenditure” columns.

**Extract Subsets of xts Objects,** Details on efficient subsetting of xts objects for maximum performance and compatibility. and key points of differentiation of the time series class xts, is the ability to subset rows This allows for natural range-based time queries without requiring prior This means that it is parsed from left to right, according to the following 1. Subsetting variables. To manipulate data frames in R we can use the bracket notation to access the indices for the observations and the variables. It is easiest to think of the data frame as a rectangle of data where the rows are the observations and the columns are the variables.

##### Comments

`str( df )`

, i.e. is`date_time`

actually a`date-time`

class or a character vector?- It is a POSIXct, so I auppose date-time?
- Thank you, this is the most simple method so far! The lubridate package seems very simple and easier than xts, I am going to read up on it.
- @user2809288 glad to be of service. I also quite like juba's suggestion below (also from
`lubridate`

). Cheers! - @user2809288 while I have nothing against
`lubridate`

package, I think it's not apples to apples comparison when you compare`lubridate`

with`xts`

.`lubridate`

is utility package while`xts`

is timeseries class - What if I wanted to select every 3rd element from that subset? How will the syntax change?
- Great example! I am a huge fan of lubridate now.
- just to update this great answer, now
`new_interval()`

is deprecated. Use`interval()`

instead.