我需要将一个 STATA 数据集导入到 R 中,并且我已经下载了“外国”包。您能否告诉我将包“加载”到 R 中的步骤以及导入 STATA 数据集的步骤?
3 回答
R helplist 风格的答案:RTFM!
国家主义风格的答案:像往常一样保存你的 Stata 文件。在 R 中,键入
help(package="foreign")
找出命令是什么。与 Stata 相关的文件将包含.dta
在其中,.dta
Stata 数据文件扩展名也是如此。read.dta(file="path/name.dta")
应该在大多数情况下工作。如果没有,请尝试将您的文件从 Stata 保存为旧版本 ( saveold filename.dta, replace
)。
顺便说一句,它是Stata,而不是STATA。它不是首字母缩略词,不像 SAS 或 SPSS……所以你不必大喊大叫。
PS正如DWin正确指出的那样,您需要加载包:
library(foreign)
我认为既然你似乎知道 R,记住这不是问题。
这取决于您所说的“下载”是什么意思。您不需要下载任何内容,因为标准 R 安装中包含“foreign”以及“base”、“stats”、“utils”、“Matrix”以及其他一些“grDevices”。无论您是否已经使用 GUI 命令之一(不必要地)安装了“外部”包,您需要做的就是:
library(foreign)
?read.dta # and run the example
I just had to deal with the same issue therefore the code:
library(foreign)
setwd(your working directory)
Please note that you have to set the working directory so that R knows where to look for your Stata dta dataset
And last the code:
read.dta("name of the dataset .dta")
A video for that topic: https://www.youtube.com/watch?v=tCkCz4cu918