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

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 29 11:29:53 CET 2005


On Tue, 29 Nov 2005, 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.

newvector <- c(3,4,5,2,5,5,5,5,5,5,5,5,5,1,5,5)[oldvector]

Check:

> oldvector <- 1:16
> c(3,4,5,2,5,5,5,5,5,5,5,5,5,1,5,5)[oldvector]
  [1] 3 4 5 2 5 5 5 5 5 5 5 5 5 1 5 5

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list