Thanks a lot Jeff and Joshua,
I can see why data.table can be used with dates ( still more work to make it
work :) )
For factors, I was hoping to get a time series of a factor ( values can be only
"abc" or "def" category ) :
2015-09-04 "abc"
2015-09-05 "abc"
2015-09-06 "def"
2015-09-07 "abc"
-----Original Message-----
From: "Joshua Ulrich" [josh.m.ulrich at gmail.com]
Date: 09/03/2015 09:43 PM
To: "ce" <zadig_1 at excite.com>
CC: "R-Help" <r-help at r-project.org>
Subject: Re: [R] merge xts objects with different data types ?
On Thu, Sep 3, 2015 at 7:40 PM, ce <zadig_1 at excite.com>
wrote:>
> Hello
>
> Let's say some questions about merging xts variables :
>
> a<- xts("abc", Sys.Date())
> b <- xts("def", Sys.Date())
> c <- xts(1, Sys.Date())
>
>> merge(a,b)
> a b
> 2015-09-03 "abc" "def"
>> merge(a,b,c)
> a b c
> 2015-09-03 NA NA 1
> Warning messages:
> 1: In merge.xts(a, b, c) : NAs introduced by coercion
> 2: In merge.xts(a, b, c) : NAs introduced by coercion
> 3: In merge.xts(a, b, c) : NAs introduced by coercion
> 4: In merge.xts(a, b, c) : NAs introduced by coercion
>
> How I can merge a, b ,c correctly ? Another example is with Binary
variables :
>
>> e<- xts(TRUE, Sys.Date())
>> e
> [,1]
> 2015-09-03 TRUE
>> merge(e,b)
> e b
> 2015-09-03 1 NA
> Warning message:
> In merge.xts(e, b) : NAs introduced by coercion
>
xts objects are a matrix with an index attribute, and you can't mix
types in a matrix. So all the objects you merge need to be the same
type. For objects a, b, and c: you need to convert c to character:
storage.mode(c) <- "character"
Also, merge.xts currently only supports n-way merges integer, numeric,
and logical types (see https://github.com/joshuaulrich/xts/issues/44).
So you need to merge a and b first, then merge that result with c.
You can do that by calling merge.xts many times, or via Reduce:
merge(merge(a,b),c)
Reduce(merge, list(a,b,c))
>
> My second question is how I can convert an xts object to factor :
>
>> d <- merge(a,b)
>> d
> a b
> 2015-09-03 "abc" "def"
>> factor(d, levels = c("abc","def"))
> a b
> abc def
> Levels: abc def
>
> Date disappears here?
>
I'm not sure what you expected; factors don't have dates. It's not
clear what you're trying to do here.
> Thanks for your help
> ce
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
--
Joshua Ulrich | about.me/joshuaulrich
FOSS Trading | www.fosstrading.com