I am trying to replace values of a vector (consisting of 15 values) by a value that is related to a matching value in a dataset (consisting of 17 rows). Here's an example The vector: v <- c(f,a,e,d,m,o,e,f,i,n,e,i,b,a,o) The dataset's columns consist of the following values d[,1] <- c(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) d[,2] <- 1:17 So I want to end up with a vector that consists of the values of the second colomn, when the value of the vector matches the value of the first colomn. Thus, I aim to end up with a vector with the following values c(6,1,5,4,13,15,5,6,9,14,5,9,2,1,15) Help is appreciated! ------------------------------------------------------------------------------ De informatie opgenomen in dit bericht kan vertrouwelijk zijn en is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht onterecht ontvangt, wordt u verzocht de inhoud niet te gebruiken en de afzender direct te informeren door het bericht te retourneren. Het Universitair Medisch Centrum Utrecht is een publiekrechtelijke rechtspersoon in de zin van de W.H.W. (Wet Hoger Onderwijs en Wetenschappelijk Onderzoek) en staat geregistreerd bij de Kamer van Koophandel voor Midden-Nederland onder nr. 30244197. Denk s.v.p aan het milieu voor u deze e-mail afdrukt. ------------------------------------------------------------------------------ This message may contain confidential information and is...{{dropped:12}}
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Sep-18 00:27 UTC
[R] Replacing matching values by related values
Try playing with match(). Something like d[match(v,d[,1]),2] Should work (untested bc I'm writing from my phone though) Michael Weylandt On Sep 17, 2011, at 4:33 PM, "Janssen, K.J.M." <K.J.M.Janssen at umcutrecht.nl> wrote:> > I am trying to replace values of a vector (consisting of 15 values) by a value that is related to a matching value in a dataset (consisting of 17 rows). > Here's an example > The vector: > v <- c(f,a,e,d,m,o,e,f,i,n,e,i,b,a,o) > > The dataset's columns consist of the following values > d[,1] <- c(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) > d[,2] <- 1:17 > > So I want to end up with a vector that consists of the values of the second colomn, when the value of the vector matches the value of the first colomn. > Thus, I aim to end up with a vector with the following values > c(6,1,5,4,13,15,5,6,9,14,5,9,2,1,15) > > Help is appreciated! > > ------------------------------------------------------------------------------ > > De informatie opgenomen in dit bericht kan vertrouwelijk zijn en is > uitsluitend bestemd voor de geadresseerde. Indien u dit bericht onterecht > ontvangt, wordt u verzocht de inhoud niet te gebruiken en de afzender direct > te informeren door het bericht te retourneren. Het Universitair Medisch > Centrum Utrecht is een publiekrechtelijke rechtspersoon in de zin van de W.H.W. > (Wet Hoger Onderwijs en Wetenschappelijk Onderzoek) en staat geregistreerd bij > de Kamer van Koophandel voor Midden-Nederland onder nr. 30244197. > > Denk s.v.p aan het milieu voor u deze e-mail afdrukt. > > ------------------------------------------------------------------------------ > > This message may contain confidential information and is...{{dropped:12}} > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.