1

I'm running code that used to work on a different Macbook on a new one with

  • OS X 10.9.5
  • R studio 0.98.1083
  • R just installed freshly (first via home-brew, now standard package)

I'm trying to open a stata file that contains German umlauts (special characters). Opening it in Rstudio under Windows worked normally.

Now I try to include that file:

library(foreign)
basis <- read.dta("myfile.dta", convert.factors =NA)[LL_basis]
Error in factor(rval[[v]], levels = tt[[ll[v]]], labels = names(tt[[ll[v]]])) : 
  invalid 'labels'; length 2 should be 1 or 1

Alternatively, the following also fails

library(lattice)
library(MASS)
library(memisc)
basis <- as.data.frame(as.data.set(Stata.file("myfile.dta")))
Error in as.data.set(Stata.file("myfile.dta")) : 
  error in evaluating the argument 'x' in selecting a method for function 'as.data.set': Error in nchar(varlabs) : invalid multibyte string 4
FooBar
  • 15,724
  • 19
  • 82
  • 171
  • convert.factors takes a boolean, i.e. `TRUE` or `FALSE`. Not sure if this is your problem, but it is a place to start. Try: `basis <- read.dta("myfile.dta", convert.factors = FALSE)` – Michael Oct 29 '14 at 18:24
  • 1
    Mac versions of R built with homebrew are known to be fragile. Read the R-admin guide for details. Unless you are much more experienced than most of us, you should use the binary R packages. – IRTFM Oct 29 '14 at 19:14
  • I see. I was only doing so because the download link on the website was broken for me: http://cran.r-project.org/bin/macosx/R-3.1.1-mavericks.pkg – FooBar Oct 29 '14 at 19:18
  • I just installed the standard package for OS X > 10.5 and I receive the same errors. – FooBar Oct 29 '14 at 19:29

1 Answers1

2

The following question was related: Error in nchar() when reading in stata file in R on Mac

The error is apparently coming from neither Stata.file nor read.dta. Neither have an encoding=latin1 option. However, adding the following at the beginning of the file fixed the issue:

Sys.setlocale('LC_ALL','C')
Community
  • 1
  • 1
FooBar
  • 15,724
  • 19
  • 82
  • 171