在Mac上的R的stata文件中读取时,nchar()错误 [英] Error in nchar() when reading in stata file in R on Mac
问题描述
我正在学习R,只是试图读取stata数据文件,但出现以下错误:
I'm learning R and am simply trying to read in a stata data file but am getting the error below:
X<-Stata.file(Stata_File)
X <- Stata.file(Stata_File)
nchar(varlabs)中的错误:无效的多字节字符串253
此处的多个Mac用户在程序中遇到此错误,但在PC上运行正常.谷歌搜索此错误似乎表明它与R软件包有关,但我找不到解决方案.有任何想法吗?谢谢您的帮助!
Multiple Mac users here are encountering this error with the program but it works fine on a PC. A google search of this error seems to say it has something to do with the R package but I can't find a solution. Any ideas? Thanks for your help!!
直到错误点的R代码如下:
The R code up to the error point is below:
Root <- "/Users/Desktop/R_Training"
PathIn <- paste(Root,"Data/Example_0",sep="/")
# The 2007 Dominican Republic household member file (96 MB)
Stata_File <- "drpr51fl.dta"
# Load the memisc package:
library(memisc)
# Set the working directory:
setwd(PathIn)
# (1) Determine which variables we want:
# The Stata.file function (from memisc) reads the "header"
# of our Stata file so you can see what it contains
# and choose the variables you want.
X <- Stata.file(Stata_File)
**Error in nchar(varlabs) : invalid multibyte string 253**
以下是我的会话信息:
R版本2.13.1(2011-07-08)
平台:x86_64-apple-darwin9.8.0/x86_64(64位)
Below is my session info:
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
语言环境: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
附带的基本软件包: [1]网格统计图形grDevices utils数据集 [7]基础方法
attached base packages: [1] grid stats graphics grDevices utils datasets [7] methods base
其他附带的软件包: [1] memisc_0.95-33 MASS_7.3-13点阵_0.19-30
other attached packages: [1] memisc_0.95-33 MASS_7.3-13 lattice_0.19-30
推荐答案
似乎文件中字符串的编码不是程序认为的那样. 我猜该文件是在PC上生成的?它包含非ACII列名或数据字符串吗?
It seems like the encoding of strings in the file isn't what the program thinks it is... I guess the file was generated on a PC? Does it contain non-ACII column names or data strings?
由于您似乎具有UTF-8编码,并且(美国/西欧)PC:通常具有latin-1,所以可能是问题所在.我希望在Linux上也遇到同样的问题(也是UTF-8).
Since you seem to have UTF-8 encoding, and (US/western europe) PC:s typically have latin-1, that could be the problem. I'd expect the same problem on Linux then (also UTF-8).
可能的解决方法: Stata.file方法是否具有编码"选项?然后,您可以尝试使用"latin1"并希望获得最好的结果.
Possible work-arounds: Does the Stata.file method have an "encoding" option? Then you might try 'latin1' and hope for the best...
另一种可能性是使用--encoding = latin1选项启动R.
Another possibility is to start R with the --encoding=latin1 option.
这篇关于在Mac上的R的stata文件中读取时,nchar()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!