appending to a vector

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

appending to a vector

dirknbr
Which one is more efficient?

x2=c()
for (i in 1:length(x)) {
 x2=c(x2,func(x[i]))
}

or

x2=x
for (i in 1:length(x)) {
 x2=func(x[i])
}

where func is any function?

Dirk
Reply | Threaded
Open this post in threaded view
|

Re: appending to a vector

Sarah Goslee
Hi Dirk,

On Thu, Apr 14, 2011 at 4:59 AM, dirknbr <[hidden email]> wrote:

> Which one is more efficient?
>
> x2=c()
> for (i in 1:length(x)) {
>  x2=c(x2,func(x[i]))
> }
>
> or
>
> x2=x
> for (i in 1:length(x)) {
>  x2=func(x[i])
> }
>
> where func is any function?

This one. Creating a [vector|matrix|dataframe] of its full final size is
more efficient than growing it incrementally.

But this is likely to be even more efficient, and is shorter and easier
to read:

x2 <- sapply(x, func)

Sarah
--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: appending to a vector

Jonathan P Daily
In reply to this post by dirknbr
Neither is the fastest method. The best way would be to vectorize func so
that it accepts and returns a vector. Many builtin R functions do this and
say so in their documentation.

The slower way would be to use one of the apply functions, such as:

?lapply
x2 <- lapply(x, func)

If you must use a for loop, your second example is faster since it does
not rewrite x2 each iteration.

An excellent reference on this subject and many other R pitfalls is 'The R
Inferno'

http://lib.stat.cmu.edu/s/Spoetry/Tutor/R_inferno.pdf 
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it."
     - Jubal Early, Firefly

[hidden email] wrote on 04/14/2011 04:59:04 AM:

> [image removed]
>
> [R] appending to a vector
>
> dirknbr
>
> to:
>
> r-help
>
> 04/14/2011 11:32 AM
>
> Sent by:
>
> [hidden email]
>
> Which one is more efficient?
>
> x2=c()
> for (i in 1:length(x)) {
>  x2=c(x2,func(x[i]))
> }
>
> or
>
> x2=x
> for (i in 1:length(x)) {
>  x2=func(x[i])
> }
>
> where func is any function?
>
> Dirk
>
> --
> View this message in context: http://r.789695.n4.nabble.com/
> appending-to-a-vector-tp3449109p3449109.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.