[R] help: program efficiency

Mike Marchywka marchywka at hotmail.com
Fri Nov 26 22:29:26 CET 2010

> Date: Fri, 26 Nov 2010 11:25:26 -0800
> From: roman.lustrik at gmail.com
> To: r-help at r-project.org
> Subject: Re: [R] help: program efficiency
> Oops, tiny mistake. Try
> lapply(X = b, FUN = function(x) {
> swn <- seq(from = 0, to = (0 + 0.01*length(x))-0.01, by = 0.01)
> out <- x + swn
> return(out)
> })
> --

The way the OP stated the question, it wasn't clear what he was really
after and what he thought was a concession to what is easy. That is,
it may seem easy to add incrementing offsets to lift degeneracy but
my earlier suggestion, just adding random numbers, is probably the shortest
R code you can make and should do what is needed. If you really want
to add a fixed amount to possibly equal integers, probably the easiest
thing to do is make a hash table, does R have a hash structure?
Then, for each element, increment the value in your hash for the element value
and add it to the element. The hash table is keyed on integer value and
the hash key retrieves a value of either last increment or number of prior occurences. 

If you had to write this as a loop,  pseudo code would be something
like this

for i=0 to n 
{  vold=hash.get(v[i]); vnew=vold+.01; hash.put(v[i],vnew); v[i]+=vnew; }


More information about the R-help mailing list