[R] import from Stata, get NA

David Winsemius dwinsemius at comcast.net
Sun Jun 9 18:57:55 CEST 2013


On Jun 9, 2013, at 7:41 AM, Ulrike Pasda wrote:

> Dear all,
> I have troubles figuring out how to convert missing values from Stata
> (treated as -1 and -2) into NAs in R.
> To read in the dta file I use: data <- read.dta("data.dta")
> 
> Is there an option to tell R to convert the -1 and -2 into NAs ?
> 

The documentation for read.dta in the foreign package suggests that "true" Stata missingness is handled. (I also did not see an 'na.strings=' argument as exists in read.table.) So perhaps you are not using Stata missing indicators and have a private convention for missing. If that is the case then:

   is.na(data$colA) <- data$colA %in% c(-1, -2)

All this assumes many things which I cannot verify (whether that column is numeric class for one). You should in  further questions to Rhelp offer dput(head(dorm)) where the `data` is is the name of the dataframe. Since `data` is an R function (as is "df") I would suggest that you use 'dfr'm for data.frames. (That is not the cause of any problems but will be confusing to readers of your code.)

-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list