Skip to content
Prev 298945 / 398503 Next

How to adjust the start of a series to zero? (i.e. subtract the first value from the sequence)

On 2012-07-02 12:25, David Winsemius wrote:
[....]

I don't think that within() is inferior to transform().
I started using it after seeing some code by Bill Venables
and I now use it fairly frequently (not as frequently as the
quite useful with(), though). I think that within() can be
more versatile than transform(); consider:

  aq <- head(airquality)
  aq1 <- transform(aq, new1 = Ozone^2,
                       new2 = new1 + 10)
  #Error in eval(expr, envir, enclos) : object 'new1' not found

  aq1 <- within(aq, {new1 <- Ozone^2;
                     new2 <- new1 + 10})

I agree that, for a single new or redefined variable,
the 'df$newvar <- ...' assignment is straightforward,
but for fairly extensive modifications, within() is
very nice. The only thing that bugs me about within()
is that the new variables are appended in "reverse"
order.

Peter Ehlers