[R] use of variable labels

Thomas Lumley tlumley at u.washington.edu
Tue Apr 8 23:32:48 CEST 2003


On Tue, 8 Apr 2003, janet rosenbaum wrote:

>
> The R documentation for some of the foreign package's functions says
> that the set of variable labels becomes attributes in the resulting
> data frame.
>
> Thus, e.g., 5="strongly agree", 4="agree", etc.
>
> I'm happy that the labels are being passed, but unfortunately, when
> R summarizes the data, it will list it only as categories, and
> doesn't deal with the corresponding numbers.  It seems as though
> the numbers attached to the categories don't exist.
>
> Is there a way to make R go back and forth between the categories and
> the corresponding numbers as Stata does, or do I just have to set
> convert.factors=FALSE ?

In this particular case I don't see why you would want the numbers, but
the function as.numeric() will extract the underlying numbers from a
factor.

eg
  mean(as.numeric(MC$sex))
or
  mean(as.numeric(MC$code))
should work, but
  mean(MC$sex=="Male")
or
  mean(MC$code=="China")
should also work and seem clearer to me.


	-thomas



More information about the R-help mailing list