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 My second question is how I can convert an xts object to factor :> d <- merge(a,b) > da b 2015-09-03 "abc" "def"> factor(d, levels = c("abc","def"))a b abc def Levels: abc def Date disappears here? Thanks for your help ce
The root of your problems lie in your assumption that xts variables act like data frames. Instead they are matrices with an index attribute. All values in a matrix must be of the same storage mode. You might want to investigate the data.table package. It is not a time series object but you can still handle time series operations and you can have any type of data. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On September 3, 2015 5:40:36 PM PDT, 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 > > >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? > >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.
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