[R] transforming factor back to numbers

Rainer M Krug rkrug at sun.ac.za
Thu Sep 21 12:07:26 CEST 2006


Thanks a lot David.

Just a suggestion - it might help if this is added to the help of factor()

Rainer

David Barron wrote:
> That's an FAQ:
> 
> 7.10 How do I convert factors to numeric?
> 
> It may happen that when reading numeric data into R (usually, when
> reading in a file), they come in as factors. If f is such a factor
> object, you can use
> 
>     as.numeric(as.character(f))
> 
> to get the numbers back. More efficient, but harder to remember, is
> 
>     as.numeric(levels(f))[as.integer(f)]
> 
> In any case, do not call as.numeric() or their likes directly for the
> task at hand (as as.numeric() or unclass() give the internal codes).
> 
> On 21/09/06, Rainer M Krug <rkrug at sun.ac.za> wrote:
>> Hi
>>
>> I generate a new dataframe by doing:
>>
>> npl.agg <- aggregate(npl$DensPlants, list(year=npl$year, sim=npl$sim),
>> mean, na.rm=TRUE )
>>
>> Now I want to plot it by using
>>
>> coplot(npl.agg$x ~ npl.agg$year | npl.agg$sim, type="l")
>>
>> but, as npl.agg$year is seen as a factor, the order of the points on the
>> x-axis (time axis) does not follow the numerical sorting 1...100, but
>> rather the text sorting of the factor npl.agg$year
>>
>> Is there any way that I can 'defactor' npl.agg$year so that I have again
>> the numerical values for year?
>>
>> Thanks
>>
>> Rainer
>>
>> -- 
>> Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation
>> Biology (UCT)
>>
>> Department of Conservation Ecology and Entomology
>> University of Stellenbosch
>> Matieland 7602
>> South Africa
>>
>> Tel:            +27 - (0)72 808 2975 (w)
>> Fax:            +27 - (0)21 808 3304
>> Cell:           +27 - (0)83 9479 042
>>
>> email:  RKrug at sun.ac.za
>>         Rainer at krugs.de
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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.
>>
> 
> 

-- 
Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation
Biology (UCT)

Department of Conservation Ecology and Entomology
University of Stellenbosch
Matieland 7602
South Africa

Tel:		+27 - (0)72 808 2975 (w)
Fax:		+27 - (0)21 808 3304
Cell:		+27 - (0)83 9479 042

email:	RKrug at sun.ac.za
       	Rainer at krugs.de



More information about the R-help mailing list