Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between `transform` and `within`

Tags:

r

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!

like image 762
Rentrop Avatar asked Mar 14 '14 23:03

Rentrop


1 Answers

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.

like image 180
G. Grothendieck Avatar answered Oct 10 '22 04:10

G. Grothendieck