[R] which is faster "for" or "apply"

Berend Hasselman bhh at xs4all.nl
Wed Dec 31 08:54:58 CET 2014


> On 31-12-2014, at 08:40, Karim Mezhoud <kmezhoud at gmail.com> wrote:
> 
> Hi All,
> I would like to choice between these two data frame convert. which is
> faster?
> 
>   for(i in 1:ncol(DataFrame)){
> 
>                    DataFrame[,i] <- as.numeric(DataFrame[,i])
>                }
> 
> 
> OR
> 
> DataFrame <- as.data.frame(apply(DataFrame,2 ,function(x) as.numeric(x)))
> 
> 

Try it and use system.time.

Berend

> Thanks
> Karim
>  Ô__
> c/ /'_;~~~~kmezhoud
> (*) \(*)   ⴽⴰⵔⵉⵎ  ⵎⴻⵣⵀⵓⴷ
> http://bioinformatics.tn/
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list