[R] factor always have type integer

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Sep 9 08:14:29 CEST 2004


On Thu, 9 Sep 2004, Erich Neuwirth wrote:

> The simple answer to my problem (are the values of a vector
> numeric or not) is is.numeric, and that is enough for
> what I need right now.
> But this way I do not get an answer discriminating between
> integers and and doubles. What is the canonical way of getting the
> type of the values of a vector?
> Is there a better way than
> valtype<-function(x)
>      typeof(ifelse(is.factor(x),levels(x),x))

PLEASE do your homework as the posting guide asks.

1) A factor is not a vector: try is.vector on one.

2) levels(x) is not the `values' of an factor object.  To transfer it to 
VBA you need to coerce it to character -- at that *is* in the FAQ.

3) Once you know you have a vector, typeof() will tell you which of the 
7 types it is.  (logical, integer, double, complex, character, raw, list) 
But you need to test first.  Most R objects are not vectors, or not just 
vectors.

-- 
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