[R] Question related to merging zoo objects and apply function
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Jul 15 17:18:15 CEST 2009
Please read the last line of every message to r-help.
In particular simplify this as much as possible and
construct some small artificial test data to illustrate.
Anyways, func is probably not what you want.
It has the same effect as
func <- function(x, j) x[72+j] + [144+j]
On Wed, Jul 15, 2009 at 9:26 AM, Sergey Goriatchev<sergeyg at gmail.com> wrote:
> Hello everyone,
>
> Say I have defined a convenience function in my code like this:
>
> func <- function(x, j){
> x[168+j] <- x[72+j]+x[144+j]
> }
>
> And now I apply it to some columns in a big zoo object like this:
>
>
> for (m in 1:24){
> combined <- merge(combined, LA1sum=apply(combined, 1, func, j=m))
> }
>
> output of this for-loop will be zoo object with columns named
> LA1sum.1, LA1sum.2, ..., LA1.sum24.
>
> If I have a vector of names like this:
>
> namesVec <- c("LA1sum", "LP1sum", "GC1sum", "LL1sum", "LN1sum",
> "SI1sum", "LX1sum", "CO1sum", "CL1sum", "QS1sum", "HO1sum",
> "NG1sum", "XB1sum", "C.1sum", "FC1sum", "LH1sum", "LC1sum", "S.1sum",
> "W.1sum", "KW1sum", "CC1sum", "KC1sum", "CT1sum", "SB1sum")
>
> What I want is in merge() for each m the name of new column to be one
> of the elements in namesVec, that is
> for m=1 I have LA1sum=apply(...,j=1)
> for m=2 I have LP1sum=apply(...,j=2)
> etc
>
> What it the way to do this?
>
> Thank you in advance for your help!
>
> Regards,
> Sergey
>
> ______________________________________________
> 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