-2

我想要的是输出应该只包含每个企业的第二高薪水条目....例如:

customer_id    name            sales firstname lastname  income   business
6            Priyank Dwivedi     2   Priyank  Dwivedi    650000     PES
4              Monika Maurya     3    Monika   Maurya    200000     ITS
1               Rahul Ranjan     3     Rahul   Ranjan    1000000    PES
7               Ambreen Khan     1   Ambreen     Khan    800000     PES
3                     P Paul     3         P     Paul    500000     ITS
5               Sunny Tiwari     2     Sunny   Tiwari    900000     Analytics
2             Mayank Agarwal     3    Mayank  Agarwal    300000     PES
8             Shashank Rawat     1  Shashank    Rawat    100000     Analytics

我想要的输出是:

customer_id    name            sales firstname lastname  income   business
4              Monika Maurya     3    Monika   Maurya    200000     ITS
8             Shashank Rawat     1  Shashank    Rawat    100000     Analytics
7               Ambreen Khan     1   Ambreen     Khan    800000     PES

这是每个企业的第二高薪水。

4

1 回答 1

2

...一种解决方案可能是:

res <- t(sapply(unique(data[, "business"]),
                function(x, data){
                  # this are the subsets
                  d <- data[x==data[, "business"], ]
                  # order it and take second
                  d[order(d[, "income"], decreasing=TRUE)[2], ] 
                }, data=data))
res

作为您的data数据

于 2013-08-06T07:59:40.483 回答