You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中筛选Producttype为3的数据并计算价格中位数

在R中筛选Producttype为3的行并计算价格中位数

针对你提供的data.table格式数据集,以下是实现需求的具体步骤:

1. 筛选Producttype为3的记录

利用data.table的行筛选语法,直接提取符合条件的行:

# 筛选出Producttype等于3的所有行
sales_filtered <- sales[Producttype == 3]

2. 计算价格中位数

有两种高效的实现方式:

方式一:基于筛选后的数据集计算

先得到筛选结果,再对Price列计算中位数:

# 计算中位数
price_median <- median(sales_filtered$Price)
# 输出结果
print(price_median)

方式二:一步式聚合计算(推荐)

无需单独保存筛选后的数据集,直接在data.table中完成筛选与计算:

# 直接计算Producttype为3的价格中位数
price_median <- sales[Producttype == 3, median(Price)]
print(price_median)

运行上述代码后,会得到结果576.5,这就是所有Producttype为3的记录的价格中位数。

内容的提问来源于stack exchange,提问作者marsupilami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 23:20:38