2

I am looking to apply a function to a data frame and then store the results of that function in a new column in the data frame.

Here is a sample of my data frame, tradeData:

Login   AL  Diff
    a   1   0
    a   1   0
    a   1   0
    a   0   1
    a   0   0
    a   0   0
    a   0   0
    a   1   -1
    a   1   0
    a   0   1
    a   1   -1
    a   1   0
    a   0   1
    b   1   0
    b   0   1
    b   0   0
    b   0   0
    b   1   -1
    c   1   0
    c   1   0
    c   0   1
    c   0   0
    c   1   -1

Where the "Diff" column is the column I am trying to add. It just just the difference between the values row(x-1) and row(x) of tradeData, grouped by Login.

Here are some samples of what I've tried:

tradeData$Diff = ave(tradeData$AL,tradeData$Login,FUN = function(x) {diff(x)}) 

and

tradeData$Diff = as.data.frame(with(tradeData,tapply(AL,Login,FUN = diff)))

I've found the following question useful thus far: R applying a function to a subset of a data frame but I am unsure how to proceed from here, as I keep getting errors.

Thanks

Community
  • 1
  • 1
Mike
  • 1,049
  • 5
  • 20
  • 46

1 Answers1

3

You can try

 with(tradeData, ave(AL, Login, FUN=function(x) -1*c(0, diff(x))))
 #[1]  0  0  0  1  0  0  0 -1  0  1 -1  0  1  0  1  0  0 -1  0  0  1  0 -1

Or an option using data.table. Convert the "data.frame" to "data.table" with setDT. Take the difference between current and next value by group (by=Login). The shift function (introduced in the new devel version) with type equals "lead" gets the next value.

 library(data.table)#data.table_1.9.5 
 setDT(tradeData)[, Diff:=AL-shift(AL, type='lead', 
                          fill=0) , by=Login][]
Arun
  • 116,683
  • 26
  • 284
  • 387
akrun
  • 874,273
  • 37
  • 540
  • 662