I'm trying to operate on a specific column in an xts object by name within a function but I keep getting an error:
Error in if (length(c(year, month, day, hour, min, sec)) == 6 && all(c(year,  : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In as_numeric(YYYY) : NAs introduced by coercion
2: In as_numeric(YYYY) : NAs introduced by coercion 
If I have an xts object:
xts1 <- xts(x=1:10, order.by=Sys.Date()-1:10)
xts2 <- xts(x=1:10, order.by=Sys.Date()+1:10)
xts3 <- merge(xts1, xts2)
Then I can select a specific column with:
xts3$xts1
With a dataframe I can pass xts3 to another function and then select a specific column with:
xts3['xts1']
But if I try to do the same thing with an xts object I get the error above. e.g.
testfun <- function(xts_data){
  print(xts_data['xts1'])
}
Called with:
testfun(xts3)
This works:
testfun <- function(xts_data){
  print(xts_data[,1])
}
But I'd really like to select by name as I can't be certain of the column order.
Can anyone suggest how to solve this?
Thanks!
xts-objects have class c("xts", "zoo"), which means they are matrices with special attributes that are assigned by their creation functions. Although $ will not succeed with a matrix, it works with xts and zoo objects thanks to the $.zoo method. (It's also not recommended to use $ inside functions because of the potential for name-evaluation-confusion and partial name matching.)   See: ?xts and examine the sample.xts object created with the first example with str:
> ?xts
starting httpd help server ... done
> data(sample_matrix)
> sample.xts <- as.xts(sample_matrix, descr='my new xts object')
> 
> str(sample.xts)
An ‘xts’ object on 2007-01-02/2007-06-30 containing:
  Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "Open" "High" "Low" "Close"
  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
  xts Attributes:  
List of 1
 $ descr: chr "my new xts object"
 class(sample.xts)
# [1] "xts" "zoo"
This explains why the earlier answer advising the use of xts3[ , "x"] or equivalently xts3[ , 1] should succeed. The [.xts function extracts the "Data" element first and then returns the either named or numbered column specified by the j-argument.
 str(xts3)
An ‘xts’ object on 2018-05-24/2018-06-13 containing:
  Data: int [1:20, 1:2] 10 9 8 7 6 5 4 3 2 1 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "xts1" "xts2"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL
> xts3[ , "xts1"]
           xts1
2018-05-24   10
2018-05-25    9
2018-05-26    8
2018-05-27    7
2018-05-28    6
2018-05-29    5
2018-05-30    4
2018-05-31    3
2018-06-01    2
2018-06-02    1
2018-06-04   NA
2018-06-05   NA
2018-06-06   NA
2018-06-07   NA
2018-06-08   NA
2018-06-09   NA
2018-06-10   NA
2018-06-11   NA
2018-06-12   NA
2018-06-13   NA
The merge.xts operation might not have delivered what you expected since the date ranges didn't overlap. It seems possible that you wanted:
> xts4 <- rbind(xts1, xts2)
> str(xts4)
An ‘xts’ object on 2018-05-24/2018-06-13 containing:
  Data: int [1:20, 1] 10 9 8 7 6 5 4 3 2 1 ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL
Note that the rbind.xts-operation failed to deliver an object with the shared column name so numeric access would be needed. (I would have expected a named "Data" element, but you/we also need to read ?rbind.xts.)
Type ?`[.xts` and you'll see that the function has a i and a j argument (among others).
i - the rows to extract. Numeric, timeBased or ISO-8601 style (see details)
j - the columns to extract, numeric or by name
You passed 'xts1' as the i argument, while it should be j. So your function should be
testfun <- function(xts_data){
  print(xts_data[, 'xts1']) # or xts3[j = 'xts1']
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With