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" Thanks in advance for all replies, Stefan Jonsson
> 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
> (m <- match(data1a, map1[ ,1]))[1] 2 3 4 5 2 2 2 2> map1[m ,2] # to return labels[1] "label1" "label2" "label3" "label4" "label1" "label1" "label1" "label1" On Mon, 2004-04-19 at 11:34, 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 > > > > Thanks in advance for all replies, > Stefan Jonsson > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >