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
You are going to have to order your values for comparison. Use 'strsplit' to split on the comma, then rejoin the data items based on the sorting order. 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?
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?
Try this: x[mapply(function(x, y)all(x %in% y), strsplit(x, ','), strsplit(trip, ','))] On Fri, Sep 18, 2009 at 9: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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O