【问题标题】:How to merge data with the same country name but different years?国名不同年份的数据如何合并?
【发布时间】:2021-03-18 07:15:52
【问题描述】:

这里是 Excel/Stata 新手!

我正在尝试在 Excel 中合并两个不同的数据集(也可以使用 Stata)。这是我要合并的两个表:

第一个表:

|---------------------|------------------|------------------|
|      Country        |   Coefficient 1  |        Year      |
|---------------------|------------------|------------------|
|      Afghanistan    |         .34      |        1999      |
|---------------------|------------------|------------------|
|      Afghanistan    |         .22      |        2010      |
|---------------------|------------------|------------------|
|      Albania        |         .7       |        1971      |
|---------------------|------------------|------------------|                 

第二桌:

|---------------------|------------------|------------------|
|      Country        |  Coefficient 2   |        Year      |
|---------------------|------------------|------------------|
|      Afghanistan    |         .10      |        1999      |
|---------------------|------------------|------------------|
|      Afghanistan    |         .4       |        2004      |
|---------------------|------------------|------------------|
|      Albania        |         .2       |        1970      |
|---------------------|------------------|------------------|      

因此,虽然国家相同,但系数来自不同年份。

这是我想要的样子:

所以年份被合并,如果两个系数在同一年,那么它们将出现在同一行。如果一年只存在一个系数,那么我希望显示年份和可用系数,而另一个系数的单元格可以为空白。 (数据如何排序并不重要)。

有人知道怎么做吗?我已经尝试过 YLookup、HLookup 和 Merge 工具,但我无法使用它!

非常感谢您的想法。

【问题讨论】:

  • 如果您可以以可重现的格式提供数据,例如使用 SSC 提供的 dataex Stata 包,其他人会更容易提供帮助。

标签: excel merge dataset stata


【解决方案1】:

您可以在 Stata 中使用merge 命令。合并 countryyear 变量。

clear

input str20 country coefficient1 year
afghanistan 0.34 1999
afghanistan 0.22 2010
albania     0.70 1971
end

tempfile data1
save `data1'

clear

input str20 country coefficient2 year
afghanistan 0.10 1999
afghanistan 0.40 2004
albania     0.20 1970
end

merge 1:1 country year using `data1'

sort country year
order country year coefficient1 coefficient2

list, sepby(country)

     +------------------------------------------------------------+
     |     country   year   coeffi~1   coeffi~2            _merge |
     |------------------------------------------------------------|
  1. | afghanistan   1999        .34         .1       matched (3) |
  2. | afghanistan   2004          .         .4   master only (1) |
  3. | afghanistan   2010        .22          .    using only (2) |
     |------------------------------------------------------------|
  4. |     albania   1970          .         .2   master only (1) |
  5. |     albania   1971         .7          .    using only (2) |
     +------------------------------------------------------------+

【讨论】:

    【解决方案2】:

    在 Excel 中,您可以使用 Power Query 组合表格:

    let
        Source1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source1,{{"Country", type text}, {"Coefficient 1", type number}, {"Year", Int64.Type}}),
    
        Source2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
        #"Changed Type2" = Table.TransformColumnTypes(Source2,{{"Country", type text}, {"Coefficient 2", type number}, {"Year", Int64.Type}}),
    
        combTbl = Table.Combine({#"Changed Type",#"Changed Type2"}),
        #"Reordered Columns" = Table.ReorderColumns(combTbl,{"Country", "Year", "Coefficient 1", "Coefficient 2"}),
        #"Grouped Rows" = Table.Group(#"Reordered Columns", {"Year", "Country"}, {{"Grouped", each _, type table [Country=nullable text, Year=nullable number, Coefficient 1=nullable number, Coefficient 2=nullable number]}}),
        #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Country", Order.Ascending}, {"Year", Order.Ascending}}),
        #"Reordered Columns1" = Table.ReorderColumns(#"Sorted Rows",{"Country", "Year", "Grouped"}),
        #"Added Custom" = Table.AddColumn(#"Reordered Columns1", "Coefficient", each List.ReplaceValue(
                {List.Max(Table.Column([Grouped],"Coefficient 1")),
                 List.Max(Table.Column([Grouped],"Coefficient 2"))},
                    null,"",Replacer.ReplaceValue)),
                    
        #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Coefficient", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
        #"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"Grouped"}),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns", "Coefficient", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Coefficient.1", "Coefficient.2"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Coefficient.1", type number}, {"Coefficient.2", type number}})
    in
        #"Changed Type1"
    

    【讨论】:

      猜你喜欢
      • 2020-02-12
      • 1970-01-01
      • 2023-01-01
      • 2021-12-25
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2017-11-07
      • 2022-01-13
      相关资源
      最近更新 更多