[R] Re placing values job

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Thu Nov 29 11:12:09 CET 2007


seanpor wrote:
> fyi On my machine match runs *much* faster...
>
>   
Not too surprising. sapply and other apply-methods are usually
expedient, but not efficient.

The  order(X)[Y] method should be faster still, although perhaps not
measurably so, and of course it is less general.


>> t0 <- Sys.time(); for (i in 1:reps) { match(Y,X) }; print(Sys.time() - t0)
>>     
> Time difference of 0.1570001 secs
>   
>> t0 <- Sys.time(); for (i in 1:reps) { sapply(Y,function(Y){which(Y==X)})
>> }; print(Sys.time() - t0)
>>     
> Time difference of 6.093 secs
>   
>> 6.09/.157
>>     
> [1] 38.78981
>
> Regards,
> Sean
>
>
> Peter Dalgaard wrote:
>   
>> Ingmar Visser wrote:
>>     
>>> does this do what you want?
>>>
>>> sapply(y,function(y){which(y==x)})
>>>   
>>>       
>> Maybe, but match(Y,X) would be more to the point.
>>
>>     
>>> 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 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.
>>>>     
>>>>         
>>> Ingmar Visser
>>> Department of Psychology, University of Amsterdam
>>> Roetersstraat 15
>>> 1018 WB Amsterdam
>>> The Netherlands
>>> t: +31-20-5256723
>>>
>>>
>>>
>>> 	[[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.
>>>   
>>>       
>> -- 
>>    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
>>   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
>>  (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45)
>> 35327918
>> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45)
>> 35327907
>>
>> ______________________________________________
>> 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.
>>
>>
>>     
>
>   


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list