1

I am getting this error. Cannot figure out why? Any advise?

library(foreign)
x <- data.frame(a = "", b = 1, stringsAsFactors = FALSE)
write.dta(x, 'x.dta')
Error in write.dta(x, "x.dta") : 
 4 arguments passed to .Internal(nchar) which requires 3
asked Aug 28, 2017 at 2:08
1
  • Also could try the readstata13 package. Commented Aug 28, 2017 at 14:19

1 Answer 1

2

The haven package works much better than foreign in this case as it will read strings (including empty strings) as string values.

library( haven )
x <- data.frame( a = "", b = 1, stringsAsFactors = FALSE )
write_dta( x, 'x.dta' )

Alternatively, if you pass parameter a a value when creating the data frame, instead of an empty string, foreign will be fine.

x <- data.frame( a = "a", b = 1, stringsAsFactors = FALSE )
write.dta( x,"y.dta" )

As you're using an older version of Stata, haven is the way to go, as you can specify the version of Stata you wish the dta file to be compatible with.

write_dta( x, 'x.dta', version = 13 )
answered Aug 28, 2017 at 2:23
Sign up to request clarification or add additional context in comments.

4 Comments

dta too modern File /Users/veeresh/Documents/R projects/superstars/allstarsstata1.dta is from a more recent version of Stata.
I am getting that error when I open the file, my stata is 13
I am still getting the same error even with the new code. I don't know why. Any idea?
I've appended my answer, have a look at the last line and use the version parameter.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.