[R] Question related to merging zoo objects and apply function

Sergey Goriatchev sergeyg at gmail.com
Wed Jul 15 15:26:30 CEST 2009


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




More information about the R-help mailing list