How to merge a daily and an intra-day XTS object? - r

I am trying to merge a daily XTS object (indexed by POSIXCT, format = "%d/%m/%Y") with an intraday XTS object (indexed by POSIXCT, format = "%d/%m/%Y %H:%M").
The intra day object doesn't have a midnight (00:00) index, but by default the merge creates one and adds the daily variable to that observation.
How can I merge the daily into the intraday, but merge to the nearest index, so I do not create a bunch of 00:00 observations in my data?

Not sure if its the best solution, but I ended up filtering out any indexes that matched in both data
x <- x[!(index(x) %in% index(y))]

Related

How do I subset and manipulate time series data with lubridate and dplyr in Rstudio?

I have loaded in time series data of creek depth and am needing to calculate total annual values (Oct-April) for each year. The following is what I have tried thus far:
depth <- read_csv("Depth.Water_Depth-_All_Instruments#GCNP_-_Robber's_Roost_Spring.EntireRecord .csv")
The following is a screenshot of the resulting data frame
enter image description here
These are my attempts at making the timestamp column (ISO_UTC) into a date class. Although, each attempt makes all values for ISO_UTC into N/A values instead of dates / times.
ymd_hm(depth$ISO_UTC)
depth$ISO_UTC<- as.Date(depth$ISO_UTC, format = "%Y-%m-%dT%H:%M")
depth$ISO_UTC<- as.POSIXct(depth$ISO_UTC, format = "%Y-%m-%d %H:%M", tz = "US/Pacific")
Please help me to put these data into usable datetime values.
Please see the above details.

How to calculate time difference in R using an dataframe

Have an large data frame where there's 2 columns (POSIXct) and need to calculate length of ride.
Dates are formatted as follows:
format: "2020-10-31 19:39:43"
Can use the difftime function, correct?
Thanks
Given your data is using the correct POSIXct format you can simply subtract two dates to get the difference. No need for additional functions.
date1 <- as.POSIXct(strptime("2020-10-31 19:39:43", format = "%Y-%m-%d %H:%M:%OS"))
date2 <- as.POSIXct(strptime("2020-10-31 19:20:43", format = "%Y-%m-%d %H:%M:%OS"))
date1 - date2
Output: Time difference of 19 mins
It depends what output format you want.
For example if you want month difference between two dates, you can use the "interval" function from library "lubridate"
library(lubridate)
interval(as.Date(df$date1),as.Date(df$date2) %/% months(1))
It also works with years, weeks, days, hours

Grouping an xts by month

I have an xts object which I have split into months using split(data, "months"). This splits my data into months of each year. I want to group my data into months regardless of the year thanks.
require(lubridate) # we will use the month() function
# basically extracts the month from a date
split(data, month(as.Date(data)))
# your code, with as.Date() we make sure it's the correct format

R: Rank method changes my index format from Date to POSIXct

I have the following problem. I have a XTS containing a date column and several valuations, which should be ranked (biggest = best rank). So my original XTS is test:
> str(index(Test))
Date[1:235], format: "1995-01-31" "1995-02-28" "1995-03-31" "1995-04-28" "1995-05-31" "1995-06-30" "1995-07-31" ...
Now, my rankValuations function:
rankValuations<-function(ValuationXTS){
#Ranks the xts object asset valuations
#ValuationXTS is a xts time series with asset valuations
#Returns an xts object with ranks (asset with the greatest valuation receives 1)
#The ties parameter results in an ascending ranking. If two share the same rank, the first in the matrix gets the first rank
ranked<-as.xts(t(apply(-ValuationXTS,1,rank, ties.method="first",na.last=TRUE)))
}
After running this my index format has changed to POSIX:
> Test<-rankValuations(Test)
> str(index(Test))
POSIXct[1:235], format: "1995-01-31" "1995-02-28" "1995-03-31" "1995-04-28" "1995-05-31" "1995-06-30" "1995-07-31" ...
And this is a big problem because in the POSIX I have now a timezone. If using later on merge.xts it never matches since the POSIX dates are 1 day prior than in the to be merged with XTS which has a Date index. So how can I stop the rank method of changing Date to POSIX?

make an intra-month sum of xts object

i have an xts object
It's a daily time series.
With endpoints(prices, 'months') i can have the position of the start and the end of every months
What i want is an xts object where every data is the sum of the daily data in every month
Which is the most efficient way to do this?

Resources