1
votes

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
1
Also could try the readstata13 package.lmo

1 Answers

2
votes

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 )