Hi All, is there any general advice about speeding up recursive functions (not mentioning 'don't use them')? Regards, Federico Calboli -- Federico C. F. Calboli Department of Epidemiology and Public Health Imperial College, St. Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 75941602 Fax +44 (0)20 75943193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com
There is some memoization code at: http://tolstoy.newcastle.edu.au/R/help/03a/6412.html On 4/2/06, Federico Calboli <f.calboli at imperial.ac.uk> wrote:> Hi All, > > is there any general advice about speeding up recursive functions > (not mentioning 'don't use them')? > > Regards, > > Federico Calboli > > -- > Federico C. F. Calboli > Department of Epidemiology and Public Health > Imperial College, St. Mary's Campus > Norfolk Place, London W2 1PG > > Tel +44 (0)20 75941602 Fax +44 (0)20 75943193 > > f.calboli [.a.t] imperial.ac.uk > f.calboli [.a.t] gmail.com > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On Sun, 2 Apr 2006, Federico Calboli wrote:> is there any general advice about speeding up recursive functions > (not mentioning 'don't use them')?Well, that's very general (did you mean recursive functions in R or C or what?). Recursion is not particularly slow in R, and you are limited to a depth of a most a few thousand. E.g.:> f <- function(x) if(x > 0) f(x-1) > system.time(for(i in 1:100) f(2000))[1] 0.59 0.00 0.60 NA NA which is 3 usec per call. One piece of advice is to keep memory usage down, as in many of the examples I have looked at the speed issue was actually a memory issue, with datasets being copied at each level. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
This is not a very generic option, but for your example you can use unique and cbind to create a new dataframe with the results i.e a<-c(1,2,3,1,5) b<-c(2,2,2,2,2) c<-c(2,3,4,2,6) f=as.integer(table(paste(a,b,c)))#Stores only frequency data.frame(unique(cbind(a,b,c)),freq=f) a b c freq 1 1 2 2 2 2 2 2 3 1 3 3 2 4 1 4 5 2 6 1 I hope this helps Francisco>From: "Philip Bermingham" <pberming at arts.ryerson.ca> >To: "Philip Bermingham" <pberming at arts.ryerson.ca> >CC: r-help <r-help at stat.math.ethz.ch> >Subject: [R] Determining frequancy of events >Date: Sun, 2 Apr 2006 18:10:37 -0400 > >I have a smiple set of values: > >a<-c(1,2,3,1,5) >b<-c(2,2,2,2,2) >c<-c(2,3,4,2,6) > >Then I use data.frame(table(paste(a,b,c)))to determine the frequancy of >each set of values occures and get the result: > > Var1 Freq >1 1 2 2 2 >2 2 2 3 1 >3 3 2 4 1 >4 5 2 6 1 > >This is exactly what I need although the paste function puts the values >into a string. I want to split the var1 into three variables while hanging >onto the frequancy count so the result would be like: > > v1 v2 v3 Freq >1 1 2 2 2 >2 2 2 3 1 >3 3 2 4 1 >4 5 2 6 1 > >What is the best way to do this? > >Thanks in advance, >Philip. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.