选择单个订单中花费的最大金额

时间:2017-02-01 08:27:33

标签: r sqldf

我对R和sqldf很新,似乎无法解决一个基本问题。我有一个包含交易的文件,其中每行代表购买的产品。

该文件如下所示:

customer_id,order_number,order_date, amount, product_name
1, 202, 21/04/2015, 58, "xlfd"
1, 275, 16//08/2015, 74, "ghb"
1, 275, 16//08/2015, 36, "fjk"
2, 987, 12/03/2015, 27, "xlgm"
3, 376, 16/05/2015, 98, "fgt"
3, 368, 30/07/2015, 46, "ade"

我需要找到每个order_number在单个交易中花费的最大金额(相同customer_id)。例如,如果是customer_id "1",则为(74+36)=110

3 个答案:

答案 0 :(得分:4)

假设数据框名为orders,以下将完成工作:

sqldf("select customer_id, order_number, sum(amount) 
       from orders 
       group by customer_id, order_number")
使用嵌套查询

更新:,以下内容将提供所需的输出:

sqldf("select customer_id, max(total) 
       from (select customer_id, order_number, sum(amount) as total 
             from orders 
             group by customer_id, order_number) 
       group by customer_id")

输出:

    customer_id max(total)
1           1        110
2           2         27
3           3         98

答案 1 :(得分:4)

如果sqldf不是严格的要求。

将您的输入视为dft,您可以尝试:

require(dplyr)
require(magrittr)
dft %>% 
    group_by(customer_id, order_number) %>% 
    summarise(amt = sum(amount)) %>% 
    group_by(customer_id) %>% 
    summarise(max_amt = max(amt))

给出:

Source: local data frame [3 x 2]
Groups: customer_id [3]

  customer_id max_amt
        <int>   <int>
1           1     110
2           2      27
3           3      98

答案 2 :(得分:1)

我们也可以使用data.table。将'data.frame'转换为'data.table'(setDT(df1)),按'customer_id','order_number'分组,我们得到'{1}}'amount',再做第二组'customer_id'并获得'Sumamount'的sum

max

或者使其更加紧凑,在按“customer_id”进行分组后,我们{'1}}'ordernumber'的'amount',循环遍历library(data.table) setDT(df1)[, .(Sumamount = sum(amount)) , .(customer_id, order_number) ][,.(MaxAmount = max(Sumamount)) , customer_id] # customer_id MaxAmount #1: 1 110 #2: 2 27 #3: 3 98 ,获取split,找到list获取'MaxAmount'

sum

或使用max

setDT(df1)[, .(MaxAmount = max(unlist(lapply(split(amount,
                      order_number), sum)))), customer_id]
#   customer_id MaxAmount
#1:           1       110
#2:           2        27
#3:           3        98