Skip to content
Prev 307607 / 398513 Next

How to replicate SAS by group processing in R

Hi,

You can also try with aggregate() or ddply()
dat2<-aggregate(dat,list(dat$stock_symbol,dat$tdate),FUN=function(x) head(x,1))
?dat2[,3:6]
#????? tdate stock_symbol expiration strike
#1 9/11/2012??????????? C? 9/16/2012???? 11
#2 9/12/2012??????????? C? 9/16/2012???? 14

library(plyr)

?ddply(dat,.(stock_symbol,tdate), function(x) head(x,1))
#????? tdate stock_symbol expiration strike
#1 9/11/2012??????????? C? 9/16/2012???? 11
#2 9/12/2012??????????? C? 9/16/2012???? 14
A.K.



----- Original Message -----
From: David Winsemius <dwinsemius at comcast.net>
To: ramoss <ramine.mossadegh at finra.org>
Cc: r-help at r-project.org
Sent: Wednesday, October 10, 2012 5:42 PM
Subject: Re: [R] How to replicate SAS by group processing in R
On Oct 10, 2012, at 11:09 AM, ramoss wrote:

            
I must have forgotten my SAS. (It was a lng time ago I will admit.)? Would that have succeeded with the inclusion of 'strike' in that 'by' list?
? ? ? tdate stock_symbol expiration strike
1 9/11/2012? ? ? ? ? ? C? 9/16/2012? ?  11
4 9/12/2012? ? ? ? ? ? C? 9/16/2012? ?  14
You must mean the 'plyr' package;? there is no "PLY'. I'm sure the 'ddply' function or data.table could do this.

Here's another way with the R 'by' function which is then row-bound using 'do.call':
? ? ? tdate stock_symbol expiration strike
1 9/11/2012? ? ? ? ? ? C? 9/16/2012? ?  11
4 9/12/2012? ? ? ? ? ? C? 9/16/2012? ?  14