1

I don't understand why split applied to xts gives a list of lists. It should return xts objects. Is there something I am missing?

data(sample_matrix)
x <- as.xts(sample_matrix)
spl<-split(x, f="days")
class(spl)
 [1] "list"
class(spl[1])
 [1] "list"
class(x)
 [1] "xts" "zoo"

Edit: I want to rebuild the xts, with a new field I calculated after splitting it into days. The problem is that now my calculations are in the "split" format, that is in a list of list. How can I "rebuild" a xts from the list of list?

Mitch76
  • 89
  • 1
  • 8

1 Answers1

3

Try

> class(spl[[1]])
[1] "xts" "zoo"

split(x, 'days') creates a list of xts objects. The [[ operator is used for extracting elements from a list.

Edit to address your edit:

do.call(rbind, spl)

(if that doesn't make sense, then provide a reproducible example)

GSee
  • 48,880
  • 13
  • 125
  • 145
  • Exactly what I needed! Many Thanks. Just to know: do.call essentially is a loop?is it optimized? – Mitch76 May 15 '12 at 15:12
  • do.call has a help page. Type `?do.call`. it takes a function as its first argument and a list of arguments for that function. So, with this example, it's like calling rbind(spl[[1]], spl[[2]], spl[[3]], ...) – GSee May 15 '12 at 15:13
  • 1
    @Mitch76: If I have answered your question, please "accept" it. Thanks. – GSee May 15 '12 at 15:28