[R] How to list the all products' information of the latest month?

Berend Hasselman bhh at xs4all.nl
Fri Mar 15 13:07:58 CET 2013


On 15-03-2013, at 10:56, Tammy Ma <metal_licaling at live.com> wrote:

> Hi,
> 
> I have data frame like this:
> 
> Product     Price    Year_Month  PE
> A                 100        201012         -2
> A                 98           201101        -3
> A                 97           201102        -2.5
> B                 110         201101        -1
> B                 100         201102        -2
> B                  90          201103        -4
> 
> 
> How can I achieve the following result using R:
> Product     Price    Year_Month  PE
> A                 97           201102        -2.5
> B                  90          201103        -4
> 

Another option is to use aggregate like this

aggregate(x, by=list(x$Product), FUN=function(z) tail(z,1))[,-1]

or

aggregate(. ~ Product, data=x, FUN=function(z) tail(z,1))


Berend



More information about the R-help mailing list