[R] Transforming character vector

Ben Bolker bbolker at gmail.com
Mon Sep 13 20:57:36 CEST 2010


LCOG1 <jroll <at> lcog.org> writes:

[snip]

> 
> I need to do the same for a fueltype where each record has a character
> representing the data instead of a numeral(as in year).  No reproducible
> code or data because i think this is pretty straight forward.  I could do
> this using a series of loops but i think there is prob a more elegant way. 
> Use of grep?  Thanks for the help

Reproducible would still help because we still have to do a bit of guessing
about what you mean.  How do you want to define the ordering on characters?
If the number of fuel types is small enough, you could just create a factor
and cut() on the numeric values , e.g.

fueltype <- factor(c("A","A","B","E","Q"),levels=LETTERS)
cut(as.numeric(fueltype),c(-0.5,13.5,26.5),labels=c("A-M","N-Z"))



More information about the R-help mailing list