如何在Stata中汇总关系数据?

编程入门 行业动态 更新时间:2024-10-24 06:34:24
本文介绍了如何在Stata中汇总关系数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法解决以下Stata编程问题:

I can't wrap my head around the following Stata programming problem:

我有一张表,列出了客户购买的所有汽车并制造:

I have a table listing all car purchases by customers and make:

Customer | Make | Price ----------------------- c1 | m1 | 1 c1 | m1 | 2 c1 | m3 | 1 c2 | m2 | 2 c3 | . | .

我想将此表转换为每个客户一个观察/行的表,列出支付的最高价格

I want to transform this into a table with one observation/row per customer, listing the maximum price paid for every make:

Customer | m1 | m2 | m3 ----------------------- c1 | 2 | 0 | 1 c2 | 0 | 1 | 0 c3 | 0 | 0 | 0

如何实现?我知道重塑宽,但是由于 c1 | m1 行。另外, c3 的缺失值也会引起麻烦。

How do I achieve this? I know reshape wide, but that doesn't work because of the doubled c1 | m1 row. Also, the missing values for c3 are causing troubles.

推荐答案

在您要执行的操作上,我建议采取一些不同的方法。例如,使用-bysort-,您可以找到每个客户的最高价格。

Depending on what you want to do, I suggest approaching this a little differently. For example using -bysort- you can find the maximum price by customer for each make.

bysort Customer Make : egen maxPrice = max( Price )

或者,您可以使用折叠根据客户查找最高价格并进行以下操作:

Or, you can use collapse to find the max price by customer and make:

collapse (max) Price, by( Customer Make )

但是,如果您确实想要使用-reshape-发布的表,则可以运行以下命令:

But, if you really want the table you posted using -reshape- you could run the following:

collapse (max) Price, by( Customer Make ) drop if Price == . reshape wide Price, i( Customer ) j( Make ) string renpfix Price

请注意,如果价格列中的数据丢失,重整将失败。我将这些观察结果放在了上面的代码中,但是您可以选择执行其他操作,例如在发布的目标表中显示时,将丢失的数据替换为零。

Note that reshape will fail if it encounters missing data in the Price column. I dropped these observations in the code above but you may choose to do something different like replace the missing data with zeros as you show in your posted target table.

更多推荐

如何在Stata中汇总关系数据?

本文发布于:2023-11-22 19:35:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618740.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   数据   如何在   Stata

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!