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

基于频率表创建各地点运输类型比例变量的技术问询

解决地点运输类型占比统计问题

数据说明

Var1格式为地点.运输类型,例如A.land对应地点A、运输类型land;Frequency为该组合的出现次数;Location_ID和Transport_Type已通过Stata拆分Var1得到。

原始数据

Var1FrequencyLocation_IDTransport_Type
A.land4Aland
A.air3Aair
A.sea2Asea
B.sea5Bsea
B.other2Bother
B.land2Bland
C.land1Cland
C.air3Cair
C.other1Cother

需求目标

按地点统计各运输类型的占比,生成Proportion_land、Proportion_sea、Proportion_air、Proportion_other四个变量。以地点A为例,预期输出如下:

LocationProportion_landProportion_seaProportion_airProportion_other
A4/92/93/90

Stata实现步骤

  1. 计算每个地点的总运输次数
bysort Location_ID: egen total_freq = sum(Frequency)
  1. 生成各运输类型的占比变量
    对每个运输类型,先匹配对应记录计算占比,再将缺失值替换为0(代表该地点无此运输类型):
// 生成land类型占比
gen Proportion_land = Frequency / total_freq if Transport_Type == "land"
bysort Location_ID: replace Proportion_land = 0 if missing(Proportion_land)

// 生成sea类型占比
gen Proportion_sea = Frequency / total_freq if Transport_Type == "sea"
bysort Location_ID: replace Proportion_sea = 0 if missing(Proportion_sea)

// 生成air类型占比
gen Proportion_air = Frequency / total_freq if Transport_Type == "air"
bysort Location_ID: replace Proportion_air = 0 if missing(Proportion_air)

// 生成other类型占比
gen Proportion_other = Frequency / total_freq if Transport_Type == "other"
bysort Location_ID: replace Proportion_other = 0 if missing(Proportion_other)
  1. 保留每个地点的唯一记录(可选)
    如果需要每个地点仅显示一行汇总结果,执行以下命令:
bysort Location_ID: keep if _n == 1
  1. 整理变量格式
    将Location_ID重命名为Location,并调整变量顺序:
rename Location_ID Location
order Location Proportion_land Proportion_sea Proportion_air Proportion_other

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 16:35:50