简体   繁体   English

data.table R 的中位数

[英]median in data.table R

I try to write a code to perform a task: "Write a function purchases.median.order.price, which has one argument: purchases, and which returns the median order value (number).我尝试编写一个代码来执行一项任务:“编写一个 function purchase.median.order.price,它有一个参数:购买,并返回订单中值(数量)。

Grouping should be done using data.table.应使用 data.table 进行分组。 Records with a non-positive amount of purchased goods (returns) are ignored.购买商品(退货)数量为非正数的记录将被忽略。

Please note that one record can correspond to several records - “positions” with the same ordernumber, and that when calculating the order value, it is necessary to take into account situations when the user bought several goods of the same type (their quantity is indicated in quantity)."请注意,一条记录可以对应多条记录——同一个订单号的“仓位”,计算订单金额时需要考虑用户购买多件同类型商品的情况(标明数量)数量)。”

sample.purchases <- data.table(price = c(100000, 6000, 7000, 5000000),
                               ordernumber = c(1,2,2,3),
                               quantity = c(1,2,1,-1),
                               product_id = 1:4)
purchases.median.order.price(sample.purchases)
# 59500

I write:我写的:

library(data.table)
sample.purchases <- data.table(price = c(100000, 6000, 7000, 5000000),
                               ordernumber = c(1,2,2,3),
                               quantity = c(1,2,1,-1),
                               product_id = 1:4)

sample.purchases[quantity>0][, price*quantity, by=ordernumber]

But it's wrong.但这是错误的。 I don't know how should I find out median?我不知道我应该如何找出中位数?

Manually by hand:手动手动:

purchases.median.order.price <- function(x){
  x <- order(x);
  n <- length(x) - 1;
  n2 <- (n/2) + 1; 
  sum(x[c(floor(n2), ceiling(n2))])/2
}

Alternative you could write a function that just calls median or quantile .或者,您可以编写一个仅调用medianquantile的 function 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM