[R] Reclassifying values within a vector to several other values

Chuck Cleland ccleland at optonline.net
Tue Nov 29 13:23:39 CET 2005


Here is another approach using recode() in the car package:

 > library(car)
 > x <- 1:16
 > recode(x, "14=1; 4=2; 1=3; 2=4; else=5")
  [1] 3 4 5 2 5 5 5 5 5 5 5 5 5 1 5 5

Roy Sanderson wrote:
> Dear List
> 
> Apologies for such a simple question:
> 
> I have a vector of 738 elements, coded with values between 1 and 16 (but
> not containing 7, 10, 11 or 13) and wish to recode value 14 to 1, 4 to 2, 1
> to 3, 2 to 4 and all other values to 5.  I've been trying to use the
> replace function (in base) and %in%, but without success.
> 
> Many thanks
> Roy
> ----------------------------------------------------------------------------
> -------
> Roy Sanderson
> Institute for Research on Environment and Sustainability
> Devonshire Building
> University of Newcastle
> Newcastle upon Tyne
> NE1 7RU
> United Kingdom
> 
> Tel: +44 191 246 4835
> Fax: +44 191 246 4998
> 
> http://www.ncl.ac.uk/environment/
> r.a.sanderson at newcastle.ac.uk
> 
> ______________________________________________
> 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
> 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list