The function I need is
valtype<-function(x)
        typeof(ifelse(is.factor(x),levels(x),x))

It is easy enough to write.
Are there any other special cases where the values
and the storage mode differ?

The background for all this is that I am transferring data
from R to Excel with VBA and I have to handle numbers
and strings differently. So I need to check for the type
of values I will get returned before I do the transfer.

Therefore, I would like to know if there are other types of variables
(besides factors) which give a misunderstandable answer about the type of their values when asked typeof.





Prof Brian Ripley wrote:

On Wed, 8 Sep 2004, Erich Neuwirth wrote:


typeof applied to a factor always seems to return "integer",
independently of the type of the levels.


typeof is telling you the internal structure. From ?factor

'factor' returns an object of class '"factor"' which has a set of
integer codes the length of 'x' with a '"levels"' attribute of
mode 'character'.


(Despite that, we don't enforce this and people have managed to create factors with non-integer numeric codes.)

Now ?typeof says

     'typeof' determines the (R internal) type or storage mode of any
     object

and that is the "integer" as the codes are stored in an INTSXP.

BTW, factors were an internal type long ago, and were one of the two
unnamed types which appear in output from memory.profile().


This has a strange side effect.


It's a very well documented feature of data.frame, as others have pointed out.


When a variable is "imported" into a data frame,
its type changes.
character variables automatically are converted
to factors when imported into data frames.

Here is an example:

> v1<-1:3
> v2<-c("a","b","c")
> df<-data.frame(v1,v2)
> typeof(v2)
[1] "character"
> typeof(df$v2)
[1] "integer"

It is somewhat surprising that
the types of v2 and df$v2 are different.

the answer is to do
levels(df$v2)[df$v2]
but that is somewhat involved.

Should the types not be identical, and typeof applied to factors
return the type of the levels?







--
Erich Neuwirth, Computer Supported Didactics Working Group
Visit our SunSITE at http://sunsite.univie.ac.at
Phone: +43-1-4277-38624 Fax: +43-1-4277-9386

______________________________________________
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to