Referencing new columns inside transform

Viewed 272

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.

3 Answers
Related