[R] recoding large number of categories (select in SAS)

Denis Chabot chabotd at globetrotter.net
Wed Jan 19 14:56:57 CET 2005


Hi,

I have data on stomach contents. Possible prey species are in the 
hundreds, so a list of prey codes has been in used in many labs doing 
this kind of work.

When comes time to do analyses on these data one often wants to regroup 
prey in broader categories, especially for rare prey.

In SAS you can nest a large number of "if-else", or do this more 
cleanly with "select" like this:
select;
   when (149 <= prey <=150)   preyGr= 150;
   when (186 <= prey <= 187)  preyGr= 187;
   when (prey= 438)                 preyGr= 438;
   when (prey= 430)                 preyGr= 430;
   when (prey= 436)                 preyGr= 436;
   when (prey= 431)                 preyGr= 431;
   when (prey= 451)                 preyGr= 451;
   when (prey= 461)                 preyGr= 461;
   when (prey= 478)                 preyGr= 478;
   when (prey= 572)                 preyGr= 572;
   when (692 <= prey <=  695 )                                       
preyGr= 692;
   when (808 <= prey <=  826, 830 <= prey <= 832 )  	preyGr= 808;
   when (997 <= prey <= 998, 792 <= prey <= 796)  	preyGr= 792;
   when (882 <= prey <= 909)  						preyGr= 882;
   when (prey in (999, 125, 994))  					       preyGr= 9994;
   otherwise                             preyGr= 1;
end; *select;

The number of transformations is usually much larger than this short 
example.

What is the best way of doing this in R?

Sincerely,

Denis Chabot




More information about the R-help mailing list