[R] row, col function but for a list (probably very easy question, cannot seem to find it though)

peter dalgaard pdalgd at gmail.com
Tue Mar 27 09:37:52 CEST 2012


On Mar 26, 2012, at 17:33 , David Winsemius wrote:

> The usual approach to that problem is to use sapply:
> 
> x <- list()
> x <- sapply(1:10, function(z) x[[z]] <- 1:z )

Yikes!

If that works, it is only by coincidence.... (The pre-assignment to x only serves the purpose of allowing the "[["-assignment inside the anonymous function, but the assignment is to a local copy which is deleted on exit, and the return value is the rhs of the assignment.) 

Please:

x <- lapply(1:10, function(z) 1:z)

or even

x <- lapply(1:10, seq_len)

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list