[R] Removing rows with earlier dates

Joshua Wiley jwiley.psych at gmail.com
Fri Dec 24 18:40:04 CET 2010


On Fri, Dec 24, 2010 at 5:45 AM, Ali Salekfard <salekfard at googlemail.com> wrote:
> I have a data frame that contains mapping rules in this way:
> The dataframe comes from a database that stores all dates. What I would like
> to do is to create a data frame with only the most recent rule for each

Assuming that "Effective Date" is a date class (you can use
str(yourdataframe) you tell):

with(YourDataFrame, tapply(`Effective Date`, `RULE COLUMNS`, which.max))

this will return the location of the maximum `Effective Date`

If you want the actual value, rather than the location:

with(YourDataFrame, tapply(`Effective Date`, `RULE COLUMNS`,
  function(x) x[which.max(x)]))

should do the trick (assuming Effective Date is a date class, not
character).  By the way, you will find your life more convenient if
you avoid object or column names with spaces or special characters.



> account. In traditional programming languages I would loop through each
> account find the most recent rule(s) and fill up my updated data frame.
> Does anyone have any better idea to use R's magic (Its syntax is still
> magical to me) for this problem?
> By the way the list of rules is quite extensive (144643 lines to be
> precise), and there are usually 1-3 most recent rules (rows) for each
> account.
> Thanks.
>        [[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.

Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles

More information about the R-help mailing list