[R] looking up value from a pair of vectors
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon Apr 19 12:48:18 CEST 2004
> match(data1a, xx)
[1] 2 3 4 5 2 2 2 2
On Mon, 19 Apr 2004, Stefann Jonsso wrote:
> Hello R community.
>
> Can anyone inform me how to solve this short problem? I have a dataset that
> I want to recode according to a pair of short numerical vectors. Here is a
> short example:
>
>
> # Start R code
>
> map1 <- matrix(ncol=3, byrow=T,c( 197796,"label0",1,
> 197797,"label1",2,
> 197798,"label2",3,
> 197799,"label3",4,
> 197800,"label4",5))
>
> xx <- as.integer(map1[,1])
> lab <- map1[,2]
> yy <- as.integer(map1[,3]
>
>
> data1a <- c(197797, 197798, 197799, 197800,
> 197797, 197797, 197797, 197797)
>
>
>
> data1b <- some.function.or.combinaion.of.few(data1a,xx,yy)
>
> # desired result for data1b
>
> data1b
> 2 3 4 5 2 2 2 2
>
> # End R code
>
>
> data1a is always longer than xx.
> all numerical entries in data1a are members of xx. (in the more general
> problem, the the number 197796 may or may not be included in data1a)
> value in data1a that are not members of xx would produce a "Na"
--
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