Skip to content

Stuck trying to modify a function

4 messages · Benjamin Ward (ENV), Milan Bouchet-Valat, Jean V Adams

#
Le mardi 27 novembre 2012 ? 16:45 +0000, Benjamin Ward (ENV) a ?crit :
You should really read about vectorizing operations: you can most likely
get the same results in R using only a few lines, with a much better
performance. runif(length(x)) will directly give you a vector of the
needed length, and you can add or subtract 1 from x in one line:

new.x <- ifelse(runif(length(x)) > .5, x + 1, x - 1)
y[match(x, y, nomatch=0)] <- new.x
y <- ifelse(y %in% x, new.x[match(y, x)], y)
x <- new.x

This is of course a proof of concept, I'm not sure this is really what
you asked for. See below for some debugging of your code.
Bug is here:       ^

You should specify an object to index.
You should have copied the full output. This is only the last error
message: once an error happens, the whole syntax is broken and every
bracket can trigger an error. Only by looking at the first error you can
understand what's the problem.


Regards