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
Hi Dirk,
On Thu, Apr 14, 2011 at 4:59 AM, dirknbr 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]
2 matches
Mail list logo