Juliane Struve
2009-Sep-22 09:52 UTC
[R] matching pairs regardless of order,multiple matches
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?
David Winsemius
2009-Sep-22 12:19 UTC
[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 <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, >> >> JulianeDavid Winsemius, MD Heritage Laboratories West Hartford, CT