[R] Equivalent of SAS's FIRST. And LAST. Variable in R?
Martyn Byng
Martyn.Byng at nag.co.uk
Tue Jul 13 09:11:18 CEST 2010
Hi,
I expect there are better ways of doing this then the following, but you
can probably get the same sort of thing using the split command
bb =
matrix(c(1,1,1,2,2,2,3,3,3,3,10,11,12,43,23,14,52,52,12,23),ncol=2,byrow
=FALSE)
aa = split(bb,bb[,1])
sapply(aa,function(temp) {cc = matrix(temp,ncol=ncol(bb));cc[nrow(cc),2]
- cc[1,2]})
where the first column of bb is the "ClientCode" the sapply gives the
difference in the second column of bb between the "last" and "first"
value for each client
Martyn
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Wang, Kevin (SYD)
Sent: 13 July 2010 07:52
To: r-help at r-project.org
Cc: Wang, Kevin (SYD); wang.kevin at gmail.com
Subject: [R] Equivalent of SAS's FIRST. And LAST. Variable in R?
Hi all,
I'm just wondering if there is a equivalent of SAS's FIRST. and LAST.
variables in R?
For example, suppose this is a snapshot of the data:
ClientCode CaseCode open close Important
1 37 28 2003-07-08 2003-09-02 1
2 37 310 2003-11-01 2004-09-10 1
3 37 1562 2007-04-03 2007-07-27 1
4 38 29 2003-02-28 2007-09-05 1
5 38 599 2004-07-14 2007-10-31 1
For each client, I want to see that if a case is important (Important =
1), then check its close date and the next open-date for the client.
E.g. for Client 37, I want to get the difference between Case 310's open
date and Case 28's close date, as well as find out whether Case 310 is
"Important".
I know how to get this using SAS's DATA step (with BY statement and
FIRST. variable). But I'm having trouble finding the equivalent in R.
Any suggestions will be greatly appreciated!
Cheers,
Kevin
Kevin Wang
Senior Adviser, Government Advisory Services
Advisory
KPMG
10 Shelley Street
Sydney NSW 2000 Australia
Tel +61 2 9335 8282
Fax +61 2 9335 7001
Mob 0404 518 301
kevinwang at kpmg.com.au <mailto:kevinwang at kpmg.com.au >
kpmg.com.au <http://kpmg.com.au/>
Best Accounting Firm - BRW Client Choice Awards 2010
Protect the environment: please think before you print
[[alternative HTML version deleted]]
______________________________________________
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.
________________________________________________________________________
This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}
More information about the R-help
mailing list