[R] Factors? I think?

Rolf Turner rolf.turner at xtra.co.nz
Fri Sep 9 02:20:56 CEST 2011


On 09/09/11 11:36, Totally Inept wrote:
> First of all, let me apologize, as this is probably an absurdly basic
> question. I did search before asking, but perhaps my ineptitude didn't allow
> me to apply what I read to what I'm doing. Totally new to R, and haven't
> done any code in any language in a long time.
>
> Basically I've got categories. They're department codes for doctors (say,
> 9999 for radiology or 5555 for endocrinology), which of course means that
> there are a good number of them, i.e. it's not practical for me to write
> them all out as I usually see in examples of categorical variables
> (factors).
>
> And then I've got a list of doctors that I'm actually interested in. I have
> the department codes associated with each, but I need to map the department
> name to the doctor name. So I might have Greg Jones, Bob Smith, Tom Wilson,
> etc... to go with 1234, 9999, 2222, etc.
>
> I need to turn Greg Jones, Bob Smith, ... and 1234, 9999, ... into Greg
> Jones, Bob Smith, ... Cardiology, Radiology, ....
>
> Obviously I could just search and replace within the csv files but I need
> something durable that I can run things through repeatedly.
>
> Anyhow, thanks to anyone willing to humor me with an answer.

Not really clear what your problem is .... I *think* that you just want
to re-code the department codes to their comprehensible name
equivalents.  You can indeed do this using factors.

Suppose that you have your data in a data frame "X", with columns
"doctor" and "department".  You want to recode "department".

You can do something like:

X$department <- factor(X$department,levels=c(1234,9999,2222,<whatever>),
                                         
labels=c("Cardiology","Radiology","IngrownToenails",<whatever>))

Given that there are a large number of departments you may well want to
put the appropriate command into a text file and source() that file.

Or you may find it more convenient to use the recode() function from the 
"car"
package.

HTH

     cheers,

             Rolf Turner



More information about the R-help mailing list