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
user3570187
1,7733 gold badges18 silver badges39 bronze badges
1 Answer 1
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 )
Sign up to request clarification or add additional context in comments.
4 Comments
user3570187
dta too modern File /Users/veeresh/Documents/R projects/superstars/allstarsstata1.dta is from a more recent version of Stata.
user3570187
I am getting that error when I open the file, my stata is 13
user3570187
I am still getting the same error even with the new code. I don't know why. Any idea?
Foxhound
I've appended my answer, have a look at the last line and use the version parameter.
lang-r
readstata13package.