[R] dput vs unclass to see what a factor really is composed of

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 1 00:00:38 CEST 2008


See ?NumericConstants.

On Thu, 31 Jul 2008, Jacob Wegelin wrote:

> I used read.dta() to read in a Stata 9 dataset to R. The "Sex01" variable
> takes on two values in Stata: 0 and 1, and it is labeled "M" and "F"
> respectively, analogous to an R factor. Thus, read.dta reads it in as a
> factor.
>
> Now, I wanted to see what this variable *really* is, in R. For instance,
> sometimes R converts a 0/1 variable into a 1/2 variable when it considers it
> a factor. The "dput" function often enables me to see what a variable really
> is, but in this case it tells me that the components of the factor are "1L"
> and "2L", that is, "one uppercase ell" and "two uppercase ell."  What does
> the uppercase ell mean?
>
> In this case, "unclass" seems to enable me to see what the variable really
> consists of. But what do "1L" and "2L" mean?
>
>> summary(DAT$Sex01[1:150])
>  M   F
> 137  13
>> dput(DAT$Sex01[1:150])
> structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L,
> 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
> 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
> 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("M", "F"), class = "factor")
>
>> unclass(junk)
>  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
> 1 2 1 1 2 2 1 1 1 1 1 2 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
> 1 1 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1
> [149] 1 1
> attr(,"levels")
> [1] "M" "F"
>
>> str(junk)
> Factor w/ 2 levels "M","F": 1 1 1 1 1 1 1 1 1 1 ...
>
>
> Jake
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list