使用 R,我需要为每个部门支出最多的前 2 名员工建立一个报告,并为该部门的其他员工添加一个“其他”。例如,我需要一份与此类似的报告。
Dept. EmployeeId Expense
Marketing 12345 100
Marketing 12346 90
Marketing Others 200
Sales 12347 50 <-- There's just one employee with expenses
Research 12348 2000
Research 12349 900
Research Others 10000
换句话说,我需要总结数据,重点关注支出最多的前 2 名员工。费用栏的总和应该是公司的费用总额。
employeIds <- sample(1000:9999, 20)
depts <- sample(c('Sales', 'Marketing', 'Research'), 20, replace = TRUE)
expenses <- sample(1:1000, 20, replace = TRUE)
df <- data.frame(employeIds, depts, expenses)
# Based on that data, how do I build a table with the top 2 employees with the most expenses in each department, including an "Other" employee per department.
我是 R 新手,我不确定如何处理这个问题。在 SQL 中,我本来可以使用 RANK() 函数和 JOIN ,但这里不是一个选项。