数据转置
-
xpose
:限于转换数值型(numeric)变量的数据- 下图标号3:一旦出现文字型(string)变量将会全部强制转为缺失值(missing)
-
varname
:保留转换前数据结构的变量名(见下图标号1)
-
sxpose
:也支持文字型变量的数据的转置(见下图标号4)- 如果全是数值型变量,加入
force
选项、实现效果与xpose
相同(见下图标号2)
- 如果全是数值型变量,加入
长宽型数据互转 (long → wide)
-
reshape
-
reshape long stub, i(i) j(j) string
-
i(i)
:括号里的i
表示作为 ID 的变量,可以唯一识别数据集 -
j(j)
:括号里的j
表示新生成的变量,即宽型数据里不存在的变量 -
string
:声明j
是字符型变量
-
-
reshpae wide stub, i(i) j(j) string
-
j(j)
:括号里的j
表示已经存在的变量
-
- 推荐阅读:扁担宽板凳长,reshape让你们都变形
-
-
gather
-
reshape
命令的简洁版 - 比如,下图同样的数据结构,
gather water*
就能搞定
-
-
sreshape
、fastreshape
:适用于大型数据集,比 reshape 快很多- 推荐阅读:reshape命令一文读懂!| 连享会
-
wdireshape
:reshapes the World Development Indicators (WDI) database for panel data -
joinby
:form all pairwise combinations within groups