[R] Strange Colnames

Gabor Grothendieck ggrothendieck at myway.com
Fri Feb 25 16:17:26 CET 2005


From:   Georg Hoermann <georg.hoermann at gmx.de>
> Gabor Grothendieck wrote:
> > 
> > Here is something you could try:
> > 
> > # define lags and their names
> > lags <- 0:4
> > names(lags) <- c("G_HW", paste("lag", 1:4))
> > 
> > # build mts
> > do.call("cbind", lapply(lags, lag, x = lagtest))
> 
> thank you for the solution,
> I will try to understand it during the weekend 8-)
> Now I tried to change the lag from the default value of 1 to -1,
> but I apparently missed something:
> 
> > do.call("cbind", lapply(lags, function(x) lag(x,-1), x = lagtest))
> Error in FUN(X[[1]], ...) : unused argument(s) ( ...)
> 
> where is the unused argument?
> 


The unused argument is:

        x = lagtest 

I think what you meant is:

     do.call("cbind", lapply(lags, function(k) lag(lagtest,-k)))

or equivalently:

     do.call("cbind", lapply(-lags, function(k) lag(lagtest,k)))

or more succintly:

   do.call("cbind", lapply(-lags, lag, x = lagtest))


The way it works is that lapply calls lags(x,k) repeatedly
always using the value of x = lagtest for the first argument.
Normally lapply repeatedly sets the first argument but since
we have already done that by specifying x=lagtest, lapply
uses the next argument k thereby setting k successively to each 
value of the lag.




More information about the R-help mailing list