[R] How to get warning about implicit factor to integer coercion? Example.

WB Kloke wb at ifado.de
Mon Feb 14 20:10:16 CET 2011


Ista Zahn <izahn <at> psych.rochester.edu> writes:

> 
> Hi,
> I think an example would be helpful. I'm not sure what behavior you
> are referring to.
> 
> best,

Here is an example:

If a data.frame of experimental results is read from an external
file, a column of strings, e.g. subject codes, is converted to a
factor by default.

If I have a second table containing additional data for the subjects
(or a big pool of subjects) with subject code as rownames, then
sometimes I want to add data looked up from the 2nd table to the
data.frame, possibly to use them as additional covariates.

The wrong way to do is:

df$age <- table2[df$Subject,"age"]

This doesn't work as expected because df$Subject is silently converted
to a number, which not meaningful for table2 except when the table
happens to list the subjects in the order of levels of df$Subject.

So only
df$age <- table2[levels(df$Subject)[as.numeric(df$Subject)],"age"]
or
df$age <- table2[as.character(df$Subject),"age"]

is expected to work correctly, as is explained in the factor() doc.

This has biten me more than once, and it will bite users I am
going to evangelize R to, too.



More information about the R-help mailing list