Stata:数据框纵向追加-append-fframeappend

发布时间:2023-05-27 阅读 985

Stata连享会   主页 || 视频 || 推文 || 知乎 || Bilibili 站

温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。

New! lianxh 命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc, ihelp, rdbalance, gitee, installpkg

课程详情 https://gitee.com/lianxh/Course

课程主页 https://gitee.com/lianxh/Course

⛳ Stata 系列推文:

PDF下载 - 推文合集

作者:郭盼亭 (厦门大学)
邮箱gpting2020@163.com


目录


1. 前言

数据框 DataFrames 是 Stata16 版本新增的一项很实用的功能,它允许我们在 Stata 内存中同时存储、操作多份数据集,并且可以将这些数据连接起来,这极大提升了数据处理的效率。特别是随着数据集的增多,每个数据集的数据量的变大,如果我们想要进一步连接或合并数据集,将对 Stata 的工作运行速度提出要求。

接下来,本文主要介绍纵向追加数据框的两个命令 appendfframeappend

2. 数据框的简单介绍

新建数据框:

. * 创建名为 newframe_domestic 的 frame
. frame create newframe_domestic 

导入数据到数据框:

. * 以stata自带的数据集auto.dta为例
. frame newframe_domestic: sysuse auto.dta

在数据框中执行命令:对数据框的数据进行运算等其他具体的命令的时候,需要指定数据框的名称,具体的操作的例子如下:

 * 对数据框newframe_domestic里边的数据进行描述
. frame newframe_domestic: des
---------------------------------------------------------
Variable   Storage  Display Value
    name      type   format label  Variable label
---------------------------------------------------------
make         str18  %-18s          Make and model
price        int    %8.0gc         Price
mpg          int    %8.0g          Mileage (mpg)
rep78        int    %8.0g          Repair record 1978
headroom     float  %6.1f          Headroom (in.)
trunk        int    %8.0g          Trunk space (cu. ft.)
weight       int    %8.0gc         Weight (lbs.)
length       int    %8.0g          Length (in.)
turn         int    %8.0g          Turn circle (ft.)
displacement int    %8.0g          Displacement (cu. in.)
gear_ratio   float  %6.2f          Gear ratio
foreign      byte   %8.0g  origin  Car origin
----------------------------------------------------------
Sorted by: foreign

. * 对数据框newframe_domestic里边的数据进行筛选
. frame newframe_domestic: keep if foreign == 0

. * 对数据框newframe_domestic里的foreign变量进行sum统计
. frame newframe_domestic: sum foreign 
 Variable | Ob  Mean  Std. dev.  Min  Max
----------+------------------------------
  foreign |  52    0         0    0    0

. * 保存数据框的数据待后续使用
. frame newframe_domestic: save newframe_domestic.dta, replace

切换到其他数据框:

. * 创建名称为newframe_foreign的数据框
. frame create newframe_foreign

. * 切换到newframe_foreign的数据框
. frame change newframe_foreign 

为了接下来介绍数据框的纵向追加,这里再生成一个只含有 foreign==1 的数据集,即 newframe_foreign.dta。

. * 读取auto.dta的数据到newframe_foreign中,并且仅保留foreign变量等于1
. frame newframe_foreign: sysuse auto.dta
. frame newframe_foreign: keep if foreign == 1

. * 保存数据框的数据待后续使用
. frame newframe_foreign: save newframe_foreign.dta, replace

展示内存中所有的 frames 的名称:目前内存中有三个数据框,其中第一个是默认的一个空数据框,第二个和第三个分别是我们刚刚建立的两个只包含国内品牌汽车的数据和国外品牌的数据框。

. frames dir 
  default            26200 x 8; NLS Women 14-24 in 1968
  newframe_domestic  52 x 12; 1978 automobile data
  newframe_foreign   22 x 12; 1978 automobile data

3. 使用 append 纵向追加

这里以 newframe_domestic.dta 和 newframe_foreign.dta 这两份数据集为例,来介绍如何应用 append 进行数据框的纵向追加。

创建名为 combined 的数据框并导入国内品牌车的数据:

. * 创建名为combined的数据框
. frame create combined  

. * 将刚刚保留了国内品牌的车的数据集导入数据框combined内
. frame combined: use newframe_domestic.dta  

使用 append 命令纵向合并国外品牌车的数据:

. frame combined: append using newframe_foreign.dta

4. 使用 fframeappend 纵向追加

除了 append 命令之外,还有一个 fframeappend 命令,也可以用来进行数据框的纵向追加。该命令的一个优点就是速度比较快,适用于大型数据框 (例如,数据框观测值 > 100000,变量个数 > 1000)。但是有一缺点,目前 fframeappend 命令在向空的数据框纵向追加数据的时候,会出现问题。

fframeappend 的语法:

fframeappend [varlist] [if] [in], using(framename) [force preserve]

其中,

  • force:将字符串附加到数字或将数字附加到字符串而没有错误;
  • preserve:主文件将在程序失败或用户按下 Break 时恢复。

创建名为 combined 的数据框并导入国内品牌车的数据:

. frame create combined  
. frame combined: use newframe_domestic.dta  

使用 fframeappend 命令进行纵向合并国外品牌车的数据:

. * 切换当前的数据框为combined
. frame change combined

. * 将newframe_foreign数据框纵向合并到combined数据框里
. fframeappend, using(newframe_foreign) force

5. 相关推文

Note:产生如下推文列表的 Stata 命令为:
lianxh 数据框 合并, m
安装最新版 lianxh 命令:
ssc install lianxh, replace

相关课程

免费公开课

最新课程-直播课

专题 嘉宾 直播/回看视频
最新专题 文本分析、机器学习、效率专题、生存分析等
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
面板模型 连玉君 直击面板数据模型 [免费公开课,2小时]
  • Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。

课程主页

课程主页

关于我们

  • Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。
  • 连享会-主页知乎专栏,700+ 推文,实证分析不再抓狂。直播间 有很多视频课程,可以随时观看。
  • 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标,输入简要关键词,以便快速呈现历史推文,获取工具软件和数据下载。常见关键词:课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法

连享会小程序:扫一扫,看推文,看视频……

扫码加入连享会微信群,提问交流更方便

✏ 连享会-常见问题解答:
https://gitee.com/lianxh/Course/wikis

New! lianxhsongbl 命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh