Look Left
Look Left

Reputation: 1335

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.

Upvotes: 8

Views: 281

Answers (3)

Ramnath
Ramnath

Reputation: 55695

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)

Upvotes: 5

G. Grothendieck
G. Grothendieck

Reputation: 269556

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

Upvotes: 2

Karsten W.
Karsten W.

Reputation: 18420

Maybe something like this

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

does it?

Upvotes: 6

Related Questions