[R] Replacing matching values by related values

David Winsemius dwinsemius at comcast.net
Sun Sep 18 15:20:57 CEST 2011


On Sep 18, 2011, at 3:56 AM, Janssen, K.J.M. wrote:

> 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?

Solve what? You never posted full working code and an explicit  
example. Unless there were actually objects named "a", "b", "c", etc.  
in your workspace then the code that started out: v <-  
c(f,a,e,d,m, .... would not have been meaningful except to hint at the  
possibility that you might be comparing character vectors. I assumed  
that d[,2] was actually letters[1:17] rather than what you wrote. It's  
especially important to indicate whehte ryou have attached any objects.

Post dput(head(d)) and dput(v) for the example part and include any  
code use to construct them.

-- 
david.

>
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list