Dear list,

apologies for not including a working example.

match() does not seem to be required for my second question. 

Using the not-working example from below I managed to do something like 

aggregate(DF$Data,by=list(Trip=listoftrips),FUN="mean")

This yields means by trip, i.e. the values I am looking for. 

Sorry for the confusion and many thanks for all help !

Juliane 
 



----- Original Message ----
From: David Winsemius <dwinsem...@comcast.net>
To: Juliane Struve <juliane_str...@yahoo.co.uk>
Cc: jim holtman <jholt...@gmail.com>; Henrique Dallazuanna <www...@gmail.com>; 
r-help@r-project.org
Sent: Tuesday, 22 September, 2009 13:19:31
Subject: Re: [R] matching pairs regardless of order,multiple matches


On Sep 22, 2009, at 5:52 AM, Juliane Struve wrote:

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

Not a working example:

> DF <-data.frame(listoftrips=c("loc1,loc2", "loc1,loc3", 
> "loc2,loc3","loc1,loc2"))
> trips=c("loc1,loc2","loc2,loc3")
>
> DF$listoftrips %in% trips
[1]  TRUE FALSE  TRUE  TRUE
> DF[DF$listoftrips %in% trips, ]

[1] loc1,loc2 loc2,loc3 loc1,loc2
Levels: loc1,loc2 loc1,loc3 loc2,loc3
> 
> 
> 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.
> 
> 
> ----- Original Message ----
> From: jim holtman <jholt...@gmail.com>
> To: Juliane Struve <juliane_str...@yahoo.co.uk>
> Cc: r-help@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_str...@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

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




______________________________________________
R-help@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.

Reply via email to