In R, if you have a list of time series data, you can subset that list to include only the data between certain time points. Here is an example of how to do that.
Let's assume you have a list of time series data called ts_list
. Each element in the list is a time series object with a start
and end
time. To subset the list so that we only keep the elements that fall within a certain time period, we can use the lapply()
function along with a function that checks the start
and end
times of each element.
For example, let's say we want to keep all time series elements that begin after January 1, 2010 and end before December 31, 2015. We can create a function to check this as follows:
main.r224 chars8 lines
This function checks if the start
date is after January 1, 2010 and if the end
date is before December 31, 2015. If both conditions are met, it returns the time series object. Otherwise, it returns NULL
.
Next, we can use the lapply()
function along with this function to subset our original ts_list
:
main.r53 chars2 lines
The resulting new_list
will only contain the time series data elements that fall within our desired time period.
gistlibby LogSnag