Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing new columns inside transform

I have a dataframe with columns labeled A,B & C. I want to add new columns that are calculated from the existing columns AND the new columns themselves. To achieve this I tried using the transform function like this:

Data = transform(Data,
          NewD = A + B,
          NewE = C * NewD
)

But this gives an error:

Error in eval(expr, envir, enclos) : object 'NewD' not found

I also tried the cbind function like this:

NewD = Data$A + Data$B,
NewE = Data$C * New$D
Data=cbind(Data,NewD,NewE)

But it gets cumbersome when the number of additional columns(functions) grows.

How can I reference NewD inside the transform function, or is there a better way to apply multiple functions like this. I want Data to contain columns A, B, C, NewD & NewE without having to call the transform funciton numerous times.

like image 763
Look Left Avatar asked Jul 16 '11 12:07

Look Left


3 Answers

Maybe something like this

d <- data.frame(a=1:5, b=6:10)
transform(d, c=tmp <- a+b, e=b*tmp)

does it?

like image 194
Karsten W. Avatar answered Nov 15 '22 12:11

Karsten W.


Hadley has a mutate function in his plyr package that does precisely this. Here is the same example used by @Karsten using mutate. I find mutate code more readable for such tasks, as it does not require any temporary assignments inside.

require(plyr)
d = data.frame(a = 1:5, b = 6:10)
mutate(d, c = a + b, d = b * c, e = c * d)
like image 43
Ramnath Avatar answered Nov 15 '22 12:11

Ramnath


Here are a couple of ways. We illustrate using the built in data frame BOD:

within

> within(BOD, { a <- Time + 1; b <- a + 1 })
  Time demand b a
1    1    8.3 3 2
2    2   10.3 4 3
3    3   19.0 5 4
4    4   16.0 6 5
5    5   15.6 7 6
6    7   19.8 9 8

my.transform

my.transform is defined here and allows one to reference new columns:

> my.transform(BOD, a = Time + 1, b = a + 1)
  Time demand a b
1    1    8.3 2 3
2    2   10.3 3 4
3    3   19.0 4 5
4    4   16.0 5 6
5    5   15.6 6 7
6    7   19.8 8 9
like image 30
G. Grothendieck Avatar answered Nov 15 '22 13:11

G. Grothendieck