[R] XTS : merge.xts seems to have problem with character vectors

Joshua Ulrich josh.m.ulrich at gmail.com
Tue Jan 4 20:51:30 CET 2011


On Tue, Jan 4, 2011 at 12:42 PM, rivercode <aquanyc at gmail.com> wrote:
>
> Hi,
>
> Please can you tell me what I am doing wrong.  When trying to merge two xts
> objects, one of which has multiple character vectors for columns...I am just
> getting NAs.
>
>> str(t)
>  POSIXct[1:1], format: "2011-01-04 11:45:37"
>
>> y2 = xts(matrix(c(letters[1:10]),5), order.by=as.POSIXct(c(t + 1:5)))
>> names(y2) = c(1,2)
>> y2
>                    1   2
> 2011-01-04 11:45:38 "a" "f"
> 2011-01-04 11:45:39 "b" "g"
> 2011-01-04 11:45:40 "c" "h"
> 2011-01-04 11:45:41 "d" "i"
> 2011-01-04 11:45:42 "e" "j"
>
>> y1 = xts(c(1:5), order.by=as.POSIXct(c(t + 1:5)))
>> y1
>                    [,1]
> 2011-01-04 11:45:38    1
> 2011-01-04 11:45:39    2
> 2011-01-04 11:45:40    3
> 2011-01-04 11:45:41    4
> 2011-01-04 11:45:42    5
>
>> merge(y1, y2)
>                    y1 X1 X2
> 2011-01-04 11:45:38  1 NA NA
> 2011-01-04 11:45:39  2 NA NA
> 2011-01-04 11:45:40  3 NA NA
> 2011-01-04 11:45:41  4 NA NA
> 2011-01-04 11:45:42  5 NA NA
>
> Warning message:
> In merge.xts(y1, y2) : NAs introduced by coercion
>
> Why do I lose the character columns ?
>

Because zoo / xts objects are matrices with an ordered index (a time
index in the case of xts).  You can't mix types in a matrix.

--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com


> Cheers,
> Chris
> --
> View this message in context: http://r.789695.n4.nabble.com/XTS-merge-xts-seems-to-have-problem-with-character-vectors-tp3174125p3174125.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list