[R] Replacing matching values by related values

Petr PIKAL petr.pikal at precheza.cz
Mon Sep 19 12:12:35 CEST 2011


Hi

Works for me, how did you tested it?

v <- sample(letters[1:10], 17, replace=T)
d<-data.frame(letters[1:10], 1:10)
d[match(v,d[,1]),2]
 [1]  8  2  6  1  7 10  4 10 10  9 10  8  1  8  6  7  8

Regards
Petr



> Předmět
> 
> Re: [R] Replacing matching values by related values
> 
> Thanks Michael.
> I tested it and it works for numeric values, but not for the 'text' 
values
> that I am comparing, thus comparing "a" with "a","b", etc.
> Any advice how I can solve it?
> 
> Thanks!
> 
> 
> -----Oorspronkelijk bericht-----
> Van: R. Michael Weylandt <michael.weylandt at gmail.com> [
> mailto:michael.weylandt at gmail.com]
> Verzonden: zo 18-9-2011 2:27
> Aan: Janssen, K.J.M.
> CC: <r-help at r-project.org>
> Onderwerp: Re: [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
> > 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.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list