Martin Morgan
2006-Jul-07 12:37 UTC
[Rd] often unnecessary duplicate in sapply / as.vector
sapply calls lapply as answer <- lapply(as.list(X), FUN, ...) which, when X is a list, causes X to be duplicated unnecessarily. The coercion is unnecessary for other mode(X) because in lapply we have if (!is.list(X)) X <- as.list(X) More generally, perhaps as.vector might not duplicate when mode(x) == mode ? Martin R version 2.4.0 Under development (unstable) (2006-07-05 r38495) x86_64-unknown-linux-gnu locale: LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=en_US;LC_COLLATE=en_US;LC_MONETARY=en_US;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C attached base packages: [1] "tools" "methods" "stats" "graphics" "grDevices" "utils" [7] "datasets" "base"
Thomas Lumley
2006-Jul-08 00:04 UTC
[Rd] often unnecessary duplicate in sapply / as.vector
On Fri, 7 Jul 2006, Martin Morgan wrote:> sapply calls lapply as > > answer <- lapply(as.list(X), FUN, ...) > > which, when X is a list, causes X to be duplicated unnecessarily. The > coercion is unnecessary for other mode(X) because in lapply we have > > if (!is.list(X)) X <- as.list(X)That looks reasonable.> More generally, perhaps as.vector might not duplicate when mode(x) == mode ? >This isn't a trivial change, because mode(x)==mode does not guarantee that as.vector(x, mode) has no effect. For example, with mode="numeric" it removes attributes. -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle