11

Reading this excellent post i came across within and transform.

Reading both help files i unfortunatly did not fully understand what the differnence is...

I tried something like:

df <- data.frame(A = runif(5), B = rnorm(5))
A=1:5
within(df, C<-A+B)
transform(df,C=A+B)

Both times the Output was:

          A          B         C
1 0.2326266  1.3237210 1.5563476
2 0.4581693 -0.2605674 0.1976018
3 0.6431078  0.5920021 1.2351099
4 0.9682578  1.1964012 2.1646590
5 0.9889942  0.5468008 1.5357950

So both seem to create a new enwironment as they are ignoring A=1:5 within the evaluation.

Thanks in advance!

Community
  • 1
  • 1
Rentrop
  • 20,979
  • 10
  • 72
  • 100

1 Answers1

17

within lets you use an earlier defined variable later but not transform:

within(BOD, { a <- demand; b <- a }) # ok
transform(BOD, a = demand, b = a) # error

Note that I had defined a variation of transform that acts more like within a number of years ago here where it is called my.transform. Using that we could write the above like this:

my.transform(BOD, a = demand, b = a) # ok

In the above examples within (or my.transform) would be better but in the following transform is better:

transform(BOD, Time = demand, demand = Time) # swap columns
within(BOD, { Time <- demand; demand <- Time }) # oops

(To perform the swap with within would require that we define a temporary.)

EDIT

my.transform is now in the gsubfn CRAN package where it is called transform2 . mutate in dplyr works from left to right.

Note that transform, transform2 and mutate each work slightly differently. The RHS transform arguments all refer to the original values. The RHS of mutate arguments refer to the most recent left-to-right value. transform2 figures out the dependencies and uses those so that a dependent can come before or after the argument in which it used.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • So, if `within` is more powerful than `transform` (and just as easy to use), and their respective help pages indicate `transform` is intended for the interactive session whereas `within` appears to have no such restriction, what's the advantage of using `transform`? – Waldir Leoncio Apr 13 '15 at 21:34
  • I almost never use `within` and use `transform` a lot so transform is really more suitable for most common usages. – G. Grothendieck Apr 13 '15 at 22:06