[EMAIL PROTECTED] wrote:
This is somewhat subtle.
Rolf's solution (here corrected to...)
a <- list()
for(i in 0:10000) a[[i+1]] <- i
is the best of the loop solutions (or at least the best I know of). The
But Bill does know a better way -- it just slipped his mind.
> system.time({ a <- list(); for(i in 0:10000) a[[i+1]] <- i})
user system elapsed
1.14 0.00 1.16
> system.time({ a <- vector("list", 10001); for(i in 0:10000) a[[i+1]]
<- i})
user system elapsed
0.11 0.00 0.12
The lesson is to create the object to be the final length
rather than growing the object.
Patrick Burns
[EMAIL PROTECTED]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
apparently similar
a <- list(0)
for(i in 1:10000) a <- c(a, list(i))
will take a lot longer, although the result is the same. For example:
system.time({
a <- list()
for(i in 0:10000) a[[i+1]] <- i
})
user system elapsed
0.59 0.00 0.59
system.time({
a <- list(0)
for(i in 1:10000) a <- c(a, list(i))
})
user system elapsed
6.87 0.00 6.89
That's a factor of about 11 times as long. The best of the lot is
a <- as.list(0:10000)
of course, but this has problems with generalisation, (which everyone
suspects is going to be needed here...).
Bill Venables.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Rolf Turner
Sent: Wednesday, 28 May 2008 1:02 PM
To: Daniel Yang
Cc: r-help@r-project.org
Subject: Re: [R] how to bind lists recursively
On 28/05/2008, at 2:43 PM, Daniel Yang wrote:
Dear all,
I want to create a list that contains 0,1,2,3, ..., 10000 as its
elements. I used the following code, which apparently doesn't work
very well.
a <- 0
for(i in 1:10000) {
a <- list(a, i)
}
The result is not what I wanted. So how to create the bind lists
recursively so that the last element would be the newly added one
while the previous elements all remain the same?
a <- list()
for(i in 1:10000) a[[i]] <- i
(The word ``bind'' is inappropriate.)
cheers,
Rolf Turner
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.