[R] matching pairs regardless of order,multiple matches

Juliane Struve juliane_struve at yahoo.co.uk
Tue Sep 22 11:52:16 CEST 2009


Dear Jim and Henrique,

thank you both for your help. I have done this but run into another problem: 
 
In the example below "loc1,loc2" occurs in the (now correct, thanks to your advice) "list" twice.  
 
trips=("loc1,loc2","loc2,loc3")
 
DF$listoftrips=("loc1,loc2", "loc1,loc3", "loc2,loc3","loc1,loc2").

 
I am now using 
 
DF$Data[match(trips,listoftrips)] to get Data associated with the trips listed in "trips". 
 
My problem is that multiple matches occur and match() seems to yield only the first match. I am interested in all matches per trip and actually would like to find the mean of DF$Data associated with each trip listed in "trips". 
 
I have searched the help files and believe that the solution is aggregate() rather than match(), but I haven't quite figured out how to apply it for this example. 
 
mean(DF$Data[match(trips,listoftrips)]) gives the mean for all trips, so that's not right. How can I get means for all trips in "trips" ???
 
Many thanks for any suggestions.
 
 
Juliane 
 
 
 
 
 
 

 Dr. Juliane Struve
Environmental Scientist
10, Lynwood Crescent
Sunningdale SL5 0BL
01344 620811 



----- Original Message ----
From: jim holtman <jholtman at gmail.com>
To: Juliane Struve <juliane_struve at yahoo.co.uk>
Cc: r-help at r-project.org
Sent: Friday, 18 September, 2009 15:02:47
Subject: Re: [R] matching pairs regardless of order

Here is an example:

> x <- c('loc1,loc2', 'loc2,loc3', 'loc2,loc1', 'loc3,loc1')
> x.s <- strsplit(x, ',')
> # now sort them
> x.s <- sapply(x.s, sort)
> # create new output
> unique(apply(x.s, 2, paste, collapse=','))
[1] "loc1,loc2" "loc2,loc3" "loc1,loc3"
>


On Fri, Sep 18, 2009 at 8:47 AM, Juliane Struve
<juliane_struve at yahoo.co.uk> wrote:
> Dear list,
>
> I am using match() to match pairs of locations, e.g. trip="loc1,loc2" from a list of such pairs, e.g. list=("loc1,loc2", "loc1,loc3", "loc2,loc3","loc2,loc1").
>
> In this example match() will match "trip" with the first element of "list", but not the 4th, because the order is reversed.
>
> How can I get a match with both ?
>
> Many thanks for any help,
>
> Juliane
>
>
>
>
> ______________________________________________
> 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 that you are trying to solve?







More information about the R-help mailing list