如何在 Stata 中重新排列数据(转置)

How to rearrange data in Stata (transpose)

我想使用特定的 Stata 代码而不是在 Excel 中重新排列以下数据集(示例 1)。第一个示例显示了我的原始数据,而第二个示例显示了我希望使用 Stata 代码获得的数据。请注意,图像中的示例是在 Excel.

中完成的

The first example (original data) The second example (the desired data)

欢迎来到 SO!

您要查找的命令是reshape long,但为了获得所需的结果,您应该清理原始数据集。我利用了 DEBY00* 代码唯一标识 TypeOfArea + TypeofStation + TypeofData 的事实,因此 DEBY 代码是您想要带入最终数据集的唯一相关信息 - 您可以将 m:1 与之后的所有信息合并.

鉴于此,你应该像

clear // I copied the example from Statalist
input str13 time str24 deby001 str27 deby002 str23 deby004 str27 deby005 str24 deby006
"TypeOfArea"    "'stv§dtisches Gebiet'" "'vorstv§dtisches Gebiet'" "'lv§ndlich stadtnah'" "'vorstv§dtisches Gebiet'" "'stv§dtisches Gebiet'"
"TypeOfStation" "'Verkehr"                 "'Hintergrund"                "'Hintergrund"            "'Hintergrund"                "'Verkehr"                
"TypeOfData"    "'H'"                      "'H'"                         "'H'"                     "'H'"                         "'H'"                     
"'01:00'"       "21.53"                    "19.7"                        "24.11"                   "31.42"                       "27.22"                   
"'02:00'"       "23.2"                     "18.38"                       "24.13"                   "21.96"                       "20.75"                   
"'03:00'"       "24.65"                    "17.94"                       "26.22"                   "21.51"                       "13.54"                   
"'04:00'"       "32.31"                    "14.96"                       "21.69"                   "29.66"                       "14"                      
"'05:00'"       "23.82"                    "15.5"                        "12.1"                    "17.16"                       "11.53"                   
"'06:00'"       "10.56"                    "14.6"                        "13.19"                   "21.46"                       "11.21"                   
end

drop if _n < 4 // drop the useless headers - see above
reshape long deby00, i(time) j(deby) // obtain the data in long format
g value = real(deby00) // keep the value in numerical format
drop deby00 // drop useless string variable
so deby value // sort in the desired order