[R] Creating a new by variable in a dataframe

arun smartpink111 at yahoo.com
Fri Oct 19 22:41:12 CEST 2012


Hi,

In addition to merge(), you can also use join()
dat1<-read.table(text="
tdate  event_tim  transaction
1/10/2012   2   14
1/10/2012   4   28
1/10/2012   6   42
1/10/2012   8   14
2/10/2012   6   46
2/10/2012   9   64
2/10/2012   8   71
3/10/2012  3   85
3/10/2012   1   14
3/10/2012   4   28
9/10/2012   5   51
9/10/2012   9   66
9/20/2012  12   84
",sep="",header=TRUE,stringsAsFactors=FALSE)
dat2<-dat1[with(dat1,order(tdate,event_tim)),]
aggres<-aggregate(dat2[,-1],by=list(tdate=dat2$tdate),tail,1)
aggres$last_trans<-"Y"
library(plyr)

join(dat2,aggres,by=intersect(names(dat2),names(aggres)),type="full")
#       tdate event_tim transaction last_trans
#1  1/10/2012         2          14       <NA>
#2  1/10/2012         4          28       <NA>
#3  1/10/2012         6          42       <NA>
#4  1/10/2012         8          14          Y
#5  2/10/2012         6          46       <NA>
#6  2/10/2012         8          71       <NA>
#7  2/10/2012         9          64          Y
#8  3/10/2012         1          14       <NA>
#9  3/10/2012         3          85       <NA>
#10 3/10/2012         4          28          Y
#11 9/10/2012         5          51       <NA>
#12 9/10/2012         9          66          Y
#13 9/20/2012        12          84          Y


A.K.

----- Original Message -----
From: ramoss <ramine.mossadegh at finra.org>
To: r-help at r-project.org
Cc: 
Sent: Friday, October 19, 2012 1:51 PM
Subject: [R] Creating a new by variable in a dataframe

Hello,

I have a dataframe w/ 3 variables of interest: transaction,date(tdate) &
time(event_tim).
How could I create a 4th variable (last_trans) that would flag the last
transaction of the day for each day?
In SAS I use:
proc sort data=all6;
by tdate event_tim;
run;
         /*Create last transaction flag per day*/
data all6;
  set all6;
  by tdate event_tim;
  last_trans=last.tdate;

Thanks ahead for any suggestions.



--
View this message in context: http://r.789695.n4.nabble.com/Creating-a-new-by-variable-in-a-dataframe-tp4646782.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list