Open joshuaulrich opened 1 year ago
I agree that this is a useful functionality, thanks for suggesting an implementation. I also think that it would be good if xts
and zoo
added this in a compatible way. Some comments:
apply()
generic:zoo::apply()
whose default method just calls base::apply()
. This would probably be most convenient for many users but shadowing functions from base
is always potentially dangerous.rowapply()
and colapply()
which is similar to rowMeans()
, rowSums()
etc. except for the camel case. I would be rather strongly in favor of calling the function argument FUN
and not func.
. This is what is most frequently used in apply-type functionality, I think.colapply()
with vector return value:apply()
rather than colapply()
.colapply()
we could add an argument that controls whether the result is anchored at the end or the beginning or somewhere in between. I think the end is the most natural default but I would expect that other choices will also be requested.I have also written to Kurt now to find out whether he thinks this is important enough to try to propose generics and default methods on Bugzilla.
This would probably be most convenient for many users but shadowing functions from
base
is always potentially dangerous.
I agree about avoiding masking functions from base
. That's part of why I opened this issue (becausse xts::rowMeans()
and xts::rowSums()
mask the base
functions. Though I do sympathize with the new generic being convenient for users.
I would be rather strongly in favor of calling the function argument
FUN
and notfunc.
Agreed.
Regarding colapply()
, it's not clear to me how it should work, because of the issues you raised. For example, what should the output look like if you call colapply(x, quantile)
? The output is a vector for each column... maybe we return a zoo/xts object with duplicate timestamps? Though IIRC, that can cause issues w/zoo.
Sorry, I just now realized that my Markdown formatting was broken and hence my suggestion (b) for multi-row colapply()
wasn't displayed properly. My suggestion was to flatten the returned matrix in the following way:
colapply.xts(x, quantile)
## Open.0% High.0% Low.0% Close.0% Open.25% High.25% Low.25% Close.25% ...
## 2007-01-11 49.88529 49.99489 49.80454 49.91333 50.00505 50.12096 49.92182 49.98901 ...
If users want the plain matrix output, they can get it via apply()
.
And duplicated time stamps lead to conceptual problems when merging different series with different numbers of duplicated time stamps. That's why zoo
warns about them.
These would be generic functions to make it easier to do row/column calculations on xts than use
apply()
and have to convert the result back to xts. This idea was prompted by #281 and many users via email, stackoverflow, etc. over the years.A proof-of-concept implementation of both is below. I can't decide between the names
rowapply()
, orapplyrows()
, or either with a separator between the words.And some example use cases: