Hallo, I have two vectors of different lengths which contain the same set of values: X < -c(2,6,1,7,4,3,5) Y <- c(1,1,6,4,6,1,4,1,2,3,6,6,1,2,4,4,5,4,1,7,6,6,4,4,7,1,2) How can I replace the values in Y with the index (!) of the corresponding values in X. So 2 appears in X in the first coordinate, so all 2’s in Y should be replaced by 1, etc. Thank you for your help, Serguei ________________________________________ Austrian Institute of Economic Research (WIFO) P.O.Box 91 Tel.: +43-1-7982601-231 1103 Vienna, Austria Fax: +43-1-7989386 Mail: Serguei.Kaniovski@wifo.ac.at http://www.wifo.ac.at/Serguei.Kaniovski [[alternative HTML version deleted]]
> I have two vectors of different lengths which contain the same set of > values: > > X < -c(2,6,1,7,4,3,5) > Y <- c(1,1,6,4,6,1,4,1,2,3,6,6,1,2,4,4,5,4,1,7,6,6,4,4,7,1,2) > > How can I replace the values in Y with the index (!) of thecorresponding> values in X. So 2 appears in X in the first coordinate, so all 2???s inY> should be replaced by 1, etc.Really easy this one: X[Y] Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:21}}
?match> X[1] 2 6 1 7 4 3 5> Y[1] 1 1 6 4 6 1 4 1 2 3 6 6 1 2 4 4 5 4 1 7 6 6 4 4 7 1 2> match(Y,X)[1] 3 3 2 5 2 3 5 3 1 6 2 2 3 1 5 5 7 5 3 4 2 2 5 5 4 3 1>On Nov 28, 2007 9:53 AM, Serguei Kaniovski <Serguei.Kaniovski at wifo.ac.at> wrote:> > Hallo, > > I have two vectors of different lengths which contain the same set of > values: > > X < -c(2,6,1,7,4,3,5) > Y <- c(1,1,6,4,6,1,4,1,2,3,6,6,1,2,4,4,5,4,1,7,6,6,4,4,7,1,2) > > How can I replace the values in Y with the index (!) of the corresponding > values in X. So 2 appears in X in the first coordinate, so all 2's in Y > should be replaced by 1, etc. > > Thank you for your help, > Serguei > > ________________________________________ > Austrian Institute of Economic Research (WIFO) > > P.O.Box 91 Tel.: +43-1-7982601-231 > 1103 Vienna, Austria Fax: +43-1-7989386 > > Mail: Serguei.Kaniovski at wifo.ac.at > http://www.wifo.ac.at/Serguei.Kaniovski > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
does this do what you want?
sapply(y,function(y){which(y==x)})
hth, Ingmar
On 28 Nov 2007, at 15:53, Serguei Kaniovski wrote:
>
> Hallo,
>
> I have two vectors of different lengths which contain the same set of
> values:
>
> X < -c(2,6,1,7,4,3,5)
> Y <- c(1,1,6,4,6,1,4,1,2,3,6,6,1,2,4,4,5,4,1,7,6,6,4,4,7,1,2)
>
> How can I replace the values in Y with the index (!) of the
> corresponding
> values in X. So 2 appears in X in the first coordinate, so all 2’s
> in Y
> should be replaced by 1, etc.
>
> Thank you for your help,
> Serguei
>
> ________________________________________
> Austrian Institute of Economic Research (WIFO)
>
> P.O.Box 91 Tel.: +43-1-7982601-231
> 1103 Vienna, Austria Fax: +43-1-7989386
>
> Mail: Serguei.Kaniovski@wifo.ac.at
> http://www.wifo.ac.at/Serguei.Kaniovski
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
Ingmar Visser
Department of Psychology, University of Amsterdam
Roetersstraat 15
1018 WB Amsterdam
The Netherlands
t: +31-20-5256723
[[alternative HTML version deleted]]
>From: Serguei Kaniovski <Serguei.Kaniovski at wifo.ac.at> >Date: 2007/11/28 Wed AM 08:53:34 CST >To: r-help at stat.math.ethz.ch >Subject: [R] Replacing values jobnewY<-sapply(1:length(Y), function(.element) match(Y[.element],X)) i hope it helps you.> >Hallo, > >I have two vectors of different lengths which contain the same set of >values: > >X < -c(2,6,1,7,4,3,5) >Y <- c(1,1,6,4,6,1,4,1,2,3,6,6,1,2,4,4,5,4,1,7,6,6,4,4,7,1,2) > >How can I replace the values in Y with the index (!) of the corresponding >values in X. So 2 appears in X in the first coordinate, so all 2??????s in Y >should be replaced by 1, etc. > >Thank you for your help, >Serguei > >________________________________________ >Austrian Institute of Economic Research (WIFO) > >P.O.Box 91 Tel.: +43-1-7982601-231 >1103 Vienna, Austria Fax: +43-1-7989386 > >Mail: Serguei.Kaniovski at wifo.ac.at >http://www.wifo.ac.at/Serguei.Kaniovski[[alternative HTML version deleted]]> >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.