我想找出在过去的10年里,哪种动物越境进入德国最多。我的表结构如下:
+------+----------+-------------+
| Year | Animal | Coming from |
+------+----------+-------------+
| 2000 | bear | netherlands |
| 2001 | ant | netherlands |
| 2001 | spider | netherlands |
| 2002 | dog | poland |
| 2003 | dinosaur | swiss |
| 2004 | ant | austria |
| 2005 | bear | poland |
| 2006 | ant | austria |
| 2007 | spider | belgium |
| 2007 | cat | luxembourg |
| 2008 | fish | belgium |
| 2008 | ant | poland |
| 2009 | dog | poland |
| 2010 | dog | netherlands |
+------+----------+-------------+这只是示例数据:我的实际表要大得多,有超过7k个条目。
我想知道的是:
这种动物每年至少来德国一次。在这里我有困难来解释它。我的意思是,如果一只熊每年来德国,不管他来自哪个国家,11年来(至少一次)。所以熊得到了11。现在我想对不同的动物进行分组,所以我得到了一个由两列组成的表格,它告诉我每年哪种动物来到德国,可能只有11年中的5年。等等。
我如何在Stata中实现这一点?
发布于 2017-01-29 07:12:26
请安装dataex,以便更好地为Stata问题提供数据示例:
ssc inst dataex 在this paper中讨论了计算不同值的原理。
这可能会有所帮助:
clear
input Year str8 Animal str11 Coming_from
2000 bear netherlands
2001 ant netherlands
2001 spider netherlands
2002 dog poland
2003 dinosaur swiss
2004 ant austria
2005 bear poland
2006 ant austria
2007 spider belgium
2007 cat luxembourg
2008 fish belgium
2008 ant poland
2009 dog poland
2010 dog netherlands
end
egen tag = tag(Coming_from Animal Year)
egen distinct = total(tag), by(Coming_from Year)
tabdisp Coming_from Year, c(distinct)
------------------------------------------------------------------------------
| Year
Coming_from | 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
------------+-----------------------------------------------------------------
austria | 1 1
belgium | 1 1
luxembourg | 1
netherlands | 1 2 1
poland | 1 1 1 1
swiss | 1
------------------------------------------------------------------------------
egen ndistinct = total(tag), by(Coming_from Animal)
tabdisp Coming_from Animal, c(ndistinct)
----------------------------------------------------------------------------
| Animal
Coming_from | ant bear cat dinosaur dog fish spider
------------+---------------------------------------------------------------
austria | 2
belgium | 1 1
luxembourg | 1
netherlands | 1 1 1 1
poland | 1 1 2
swiss | 1
----------------------------------------------------------------------------https://stackoverflow.com/questions/41915495
复制相似问题