[R] value labes Stata vs factors

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sat May 2 09:45:59 CEST 2009

Rob Bakker wrote:
> Dear Sir/Madam,
> I converted my Stata Rgenmetvl.dta file with read.dta succesfully in R's
> Rgenmetvl.Rdata. However, factors give problem in certain calculations,
> e.g.:
>> mean(Rgenmetvl$sex)
> [1] NA
> Warning message:
> In mean.default(Rgenmetvl$sex) :
>   argument is not numeric or logical: returning NA
> What can I do?

Factors can't be averaged. You can average a score based on a factor 
though, as in




These do the same thing, but the former allows you more control of the 
scores asssigned. Notice that the values need to be in the order of 

Alternatively, use read.dta(...., convert.factors=FALSE) to get a 
version of data containing the original numeric values.

    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907

More information about the R-help mailing list