I am currently working with futures dataset across asset classes - which involves xts objects with numeric and character inputs. I am applying merge() to align the dataset to consistent dates, however, merge() on a xts object with character inputs, as is the case with the below example, gives NAs. Is there a work around?
Below is the dput output of a sample xts object (underlying future contracts):
uContracts <- structure(c("SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA",
"SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA",
"SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA",
"SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA",
"SPM00-USA"), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", index = structure(c(946598400,
946857600, 946944000, 947030400, 947116800, 947203200, 947462400,
947548800, 947635200, 947721600, 947808000, 948153600, 948240000,
948326400, 948412800, 948672000, 948758400, 948844800, 948931200,
949017600), tzone = "UTC", tclass = "Date"), .Dim = c(20L, 3L
), .Dimnames = list(NULL, c("SP00-USA", "SP.1-USA", "SP.2-USA"
)))
Dput out of sample dates:
tW <- structure(c(10956, 10959, 10960, 10961, 10962, 10963, 10966,
10967, 10968, 10969, 10970, 10973, 10974, 10975, 10976, 10977,
10980, 10981, 10982, 10983), class = "Date")
I would like to format uContracts as per dates in tW, wherein any dates in tW that are not in uContracts, fill underlying contract name from the last available date. I am currently doing that as below:
adjContracts <- merge(uContracts, tW, fill = na.locf)
The above command works for numeric data (such as prices), but falters for character data. My current output is:
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), .Dim = c(21L, 3L), .Dimnames = list(NULL,
c("SP00.USA", "SP.1.USA", "SP.2.USA")), index = structure(c(946598400,
946857600, 946944000, 947030400, 947116800, 947203200, 947462400,
947548800, 947635200, 947721600, 947808000, 948067200, 948153600,
948240000, 948326400, 948412800, 948672000, 948758400, 948844800,
948931200, 949017600), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC")
From what I gather, xts is converting the underlying dataset to numeric (from character). I believe, Darren Cook (PS - I hope you are okay with the shoutout) in merge.xts not merging all data using R touched upon this issue, but I am not sure how to apply it here.
Any help would be appreciated.
Cheers, S
update: below is the expected output (note the difference in the index of the xts object):
structure(c("SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA", "SPH00-USA",
"SPH00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA",
"SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA",
"SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA",
"SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA", "SPM00-USA",
"SPM00-USA"), .Dim = c(20L, 3L), .Dimnames = list(NULL, c("SP00-USA",
"SP.1-USA", "SP.2-USA")), index = structure(c(946598400, 946857600,
946944000, 947030400, 947116800, 947203200, 947462400, 947548800,
947635200, 947721600, 947808000, 948067200, 948153600, 948240000,
948326400, 948412800, 948672000, 948758400, 948844800, 948931200
), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC")