lbraglia / RStata

[R package]: A R-Stata interface
112 stars 37 forks source link

NA in chr vector translated to "NA" in string variable #7

Closed eivindhammers closed 7 years ago

eivindhammers commented 7 years ago

I have a data.frame with a character vector, which I use to generate an indicator variable in Stata, like

gen y = !missing(x)

However, stata() converts my character vector x to a Stata string variable x with NAs translated to "NA" instead of "", which I expected. Even though it's stated in ?foreign::write.dta that "missing values are handled correctly", this seems not to be the case for character vectors. May I suggest switching to haven::write_dta?

lbraglia commented 7 years ago

reproduced ...

library(RStata) 
db <- data.frame(x = c(letters[1:3], NA_character_))
(stata('gen y = !missing(x)', data.in = db, data.out = TRUE))

 . gen y = !missing(x)
    x y
 1  a 1
 2  b 1
 3  c 1
 4 NA 1

As you point out this is a foreign::write.dta problem (anyway I think this should definitely be addressed); regarding haven for data IO i wrote some lines some time ago ( https://github.com/lbraglia/RStata/issues/1 ), i want to think about it...

lbraglia commented 7 years ago

not an RStata issue. closing