[R] matching pairs regardless of order,multiple matches
Juliane Struve
juliane_struve at yahoo.co.uk
Tue Sep 22 14:45:31 CEST 2009
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 <dwinsemius at comcast.net>
To: Juliane Struve <juliane_struve at yahoo.co.uk>
Cc: jim holtman <jholtman at gmail.com>; Henrique Dallazuanna <wwwhsd at gmail.com>; r-help at 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 <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
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list