-2

I have a dataset that needs to filtered for only top3 records by the $ amount.

My table looks this

PK    Key  Amount 
111   123  $50
111   134  $150
111   154  $550
111   166  $660
111   177  $635
123   145  $245
123   157  $545
123   865  $756
123   875  $765
123   986  $976

I need to sort this table to get top 3 Key by $amount.

My End table should be something like this

PK    Top3Key 
111   166,177,154
123   986,875,865

Thanks for the help.!

user31264
  • 5,774
  • 3
  • 18
  • 35
  • [Perhaps this one](http://stackoverflow.com/questions/14800161/how-to-find-the-top-n-values-by-group-or-within-category-groupwise-in-an-r-dat) – ErrantBard Mar 21 '17 at 14:12

1 Answers1

0

If your data is named df, do

library(dplyr)
df %>%
    group_by(PK) %>%
    top_n(3, Amount) %>% 
    summarise(Top3Key = paste(Key, collapse = ','))
Ryan
  • 854
  • 6
  • 14