【问题标题】:create dataset based on all possible pairs of identifiers within each group in Stata根据Stata中每个组内所有可能的标识符对创建数据集
【发布时间】:2021-03-29 09:37:48
【问题描述】:

我有一个如下所示的数据集:

country1 country2 group
China Philippines 68a
China Thailand 68a
Bahamas Jamaica 176a
Bahamas Grenada 176a

我需要把上面的数据集改成这样:

country1 country2 group
China Philippines 68a
China Thailand 68a
Philippines China 68a
Philippines Thailand 68a
Thailand China 68a
Thailand Philippines 68a
Bahamas Jamaica 176a
Bahamas Grenada 176a
Jamaica Bahamas 176a
Jamaica Grenada 176a
Grenada Bahamas 176a
Grenada Jamaica 176a

我已尽力遵循本文中的 Stata 代码:https://www.stata.com/support/faqs/data-management/expanding-datasets-to-all-pairs/。然而,我最终得到了一个如下所示的数据集:

country1 country2 group
China Philippines 68a
China Philippines 68a
China Thailand 68a
China Thailand 68a
Bahamas Jamaica 176a
Bahamas Jamaica 176a
Bahamas Grenada 176a
Bahamas Grenada 176a

我不确定自己做错了什么。

【问题讨论】:

    标签: stata


    【解决方案1】:

    我认为问题在于您的唯一标识符实际上是两列(country1country2)的组合,而在您下面的示例中,有一个唯一的 id 列。如果您的数据集不是非常大,我将按照您的示例执行此操作:

    clear
    input str40(country1 country2 group)
    "China" "Philippines"   "68a"
    "China" "Thailand"  "68a"
    "Bahamas"   "Jamaica"   "176a"
    "Bahamas"   "Grenada"   "176a"
    end
    
    egen pair_id = group(country1 country2) // Create unique pair id
    reshape long country, i(group pair_id) j(j ) // reshape all countries long
    drop pair_id j
    rename country country1
    
    * create duplicate dataset to fulljoin
    preserve
        rename country country2
        keep country2 group
        tempfile cross
        save `cross', replace
    restore
    
    joinby group using `cross' // full join
    drop if country1 == country2
    
    * Some tidying to match example output
    order country1 country2 group
    gsort -group country1 country2
    duplicates drop
    
    

    【讨论】:

      【解决方案2】:

      我能够使用下面的代码生成我想要的数据集,但我希望找到一种更直接的编码方式。

      use "original dataset", clear
      drop country2
      save "temp_country1", replace
      
      use "original dataset", clear
      drop country1
      ren country2 country1 
      append using "temp_country1"
      
      //drop duplicates//
      sort number country1
      quietly by number country1:  gen dup = cond(_N==1,0,_n)
      drop if dup>1
      drop dup
      save "temp_country1_final", replace
      
      use "temp_country1_final", clear
      ren country1 country2
      save "temp_country2.dta", replace
      
      use "temp_country1_final", clear
      joinby number using "temp_country2.dta"
      order country1 country2  number name
      drop if country1==country2
      

      【讨论】:

      • 最后一个命令drop if country1=="country2" 对我来说看起来很不对劲。双引号没有意义,其目的显然需要drop if country1 == country2。这是微不足道的,但你还随便编辑什么?更重要的是,@JR96 已经发布了一个解决方案,所以一个好的答案会将你的和他们的比较一下。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2016-05-14
      相关资源
      最近更新 更多