[R] How do I coerce numeric factor columns of data frame to vector?
Martin Maechler
maechler at stat.math.ethz.ch
Tue Sep 9 17:37:22 CEST 2003
>>>>> "NaLi" == Na Li <nali at biostat.umn.edu>
>>>>> on Tue, 09 Sep 2003 10:15:22 -0500 writes:
NaLi> On 9 Sep 2003, Michael A. Miller outgrape:
>> > > > > > "Thomas" == Thomas W Blackwell <tblackw at umich.edu> writes:
>>
>> > Michael - Because these columns are factors to begin with,
>> > using as.numeric() alone will have unexpected results. See
>> > the section "Warning:" in help("factor").
>>
>> Ah, thanks for pointing that out. When I've used as.numeric on
>> factors, it is usually because I've explicitly made a numeric
>> variable a factor. I can see where something that I think aught
>> to be numeric would be interpreted as a factor, say due to an
>> error in formatting an input file. If I do something like this:
>>
>> x <- some numeric array
>> f <- factor(x)
>> y <- as.numeric(x)
>>
>> is there a possibility of y not equalling x? If x really is
>> numeric that is.
>> x <- c(1, 3, 1)
>> y <- factor (x)
>> y
NaLi> [1] 1 3 1
NaLi> Levels: 1 3
>> as.numeric (y)
NaLi> [1] 1 2 1
NaLi> I don't know a good way to convert it back however,
this is almost a FAQ,
but I'm astonished that the answer isn't even in help(factor).
I'll put it there for R 1.8.0
NaLi> except
>> as.numeric (levels (y)[as.numeric (y)])
NaLi> [1] 1 3 1
NaLi> which is a bit awkward.
NaLi> Michael
as.numeric(as.character(y)) !
{in some cases you might consider using as.integer() instead of as.numeric()}
More information about the R-help
mailing list