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 <[email protected]>
To: Juliane Struve <[email protected]>
Cc: [email protected]
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
<[email protected]> 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

______________________________________________
[email protected] 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