在R中聚合后将ID获取为列表

在R中聚合后将ID获取为列表,r,dataframe,R,Dataframe,我有一个包含以下列的主数据框: location_id order_id created_at pos user_id spend_amount earn_amount ref name street_address city state time date month 109936 5536 32684814 2016-06-20 17:21:56 sw?etgreen 2243440 974 900 12 - 19th + L 19th + L 1901 L St NW Washington

我有一个包含以下列的主数据框:

location_id order_id created_at pos user_id spend_amount earn_amount ref name street_address city state time date month
109936 5536 32684814 2016-06-20 17:21:56 sw?etgreen 2243440 974 900 12 - 19th + L 19th + L 1901 L St NW Washington DC 17:21:56 2016-06-20 Jun
我已将其聚合到多个子数据帧中

   AmountByUser<-aggregate(total$spend_amount, by=list(Category=total$user_id), FUN=sum)
   colnames(AmountByUser) <- c("User_Id", "Total Amount Spent")

      User_Id    Total Amount Spent
99696  3435653 46450628
207341 4821392 39621941
177899 4308353 11401622
177907 4308520 11034094
177906 4308515 8536865
177905 4308497 8324570
236885 5407939 7090316
110781 3532013 6187870
118742 3612960 4498527
236889 5407986 3441924
105507 3469230 1603637

如何获取此子数据帧顶部n%的行索引,然后将其作为主数据帧的子集?最终目标,拥有主数据框的所有原始列,并且只有支出最高的用户ID的行

实际上,您根本不需要行索引。 只需取聚合的前n个,并在整个数据帧上使用%in%运算符

topUser = AmountByUser$User_Id[1:20]
topAllData = allData[allData$user_id %in% topUser,]

这取的是前20行,而不是前20行。这取的是聚合的前20行,这意味着这些是包含前20行值的行。在显示的数据中,聚合的顺序是按花费的总额递减。如果未订购,请在挑选前20行之前进行订购。orderedData=AmountByUser[orderAmountByUser[,2],Desculation=TRUE],我在R studio中手动订购了它,实际上并没有打电话订购。谢谢