Link to new columns inside conversion

I have a framework with columns labeled A, B, and C. I want to add new columns that are computed from existing columns AND the new columns themselves. For this, I tried to use the conversion function as follows:

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

But this gives an error:

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

I also tried the cbind function as follows:

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

But this becomes cumbersome when the number of additional columns (functions) grows.

How can I refer to NewD inside the conversion function, or is there a better way to apply several such functions. I want the data to contain columns A, B, C, NewD and NewE without having to repeatedly reference the transform function.

+8
variables r transform
source share
3 answers

Hadley has a mutate function in its plyr package, which does just that. Here is the same example used by @Karsten with mutate . I find mutate code more readable for such tasks, since 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) 
+5
source share

Maybe something like this

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

this is?

+6
source share

Here are some ways. We will illustrate the use of the built-in BOD data frame:

inside

 > within(BOD, { a <- Time + 1; b <- a + 1 }) Time demand ba 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 defined here and allows you to reference new columns:

 > my.transform(BOD, a = Time + 1, b = a + 1) Time demand ab 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 
+1
source share

All Articles