[Rd] should lapply preserve attributes?

Prof Brian D Ripley ripley@stats.ox.ac.uk
Tue, 19 Mar 2002 21:31:35 +0000 (GMT)


On 19 Mar 2002, Timothy H. Keitt wrote:

> I have an application where I need to preserve object attributes across
> calls to 'lapply'. The current definition is:
>
> lapply <- function (X, FUN, ...)
> {
>     FUN <- match.fun(FUN)
>     if (!is.list(X))
>         X <- as.list(X)
>     rval <- .Internal(lapply(X, FUN))
>     names(rval) <- names(X)
>     return(rval)
> }
>
> Would it make sense to replace
>
> 	names(rval) <- names(X)
>
> with
>
> 	attributes(rval) <- attributes(X)
>
> ?? I can, of course, make a local function for this, but wondered if
> this change would be useful in general.

No, it would be positively harmful.  X might be a data frame, or a time
series .., but rval is only guaranteed to be the same length as X.  If you
know more, adjust the object returned by lapply.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._