Hi, We can match one numerical value as follows> 3 %in% c(4,5)[1] FALSE> 3 %in% c(4,5,3)[1] TRUE To see whether value pairs are identical,> identical(c(3,4), c(3,5))[1] FALSE> identical(c(3,4), c(3,4))[1] TRUE Is there any way to test whether ?A value pair is in a set of value pairs?? For example, can we test whether the pair (2,3) is identical to one of the pairs in the set S={(1,2), (4,3), (3,3), (2,3), (4,5)}? In this case, the answer is yes because the 4th element of S is (2,3). Is there any simple way to code it? Thanks! John [[alternative HTML version deleted]]
On 12/11/2016 8:36 PM, John wrote:> Hi, > > We can match one numerical value as follows >> 3 %in% c(4,5) > [1] FALSE >> 3 %in% c(4,5,3) > [1] TRUE > > To see whether value pairs are identical, >> identical(c(3,4), c(3,5)) > [1] FALSE >> identical(c(3,4), c(3,4)) > [1] TRUE > > Is there any way to test whether ?A value pair is in a set of value > pairs?? For example, can we test whether the pair (2,3) is identical to one > of the pairs in the set S={(1,2), (4,3), (3,3), (2,3), (4,5)}? > In this case, the answer is yes because the 4th element of S is (2,3). > Is there any simple way to code it? Thanks!You'll have to type a long expression or write your own function for it. Here's one way, if you store your set as a list: inlist <- function(x, thelist) any(sapply(thelist, identical, x)) For example: > S <- list(c(1, 2), c(4, 3)) > inlist(c(4, 3), S) [1] TRUE > inlist(c(3, 4), S) [1] FALSE Duncan Murdoch
This really depends on the way you represent these pairs. Here is some food for thought: p <- matrix( c( 1,2, 4,3, 3,3, 2,3, 4,5 ), byrow=TRUE, ncol=2 ) ( 2 %in% p[,1] ) & ( 3 %in% p[,2] ) # (2,3) ( c( 2, 1, 5 ) %in% p[,1] ) & ( c( 3, 2, 5 ) %in% p[,2] ) # (2,3) (1,2) (5,5) -- Sent from my phone. Please excuse my brevity. On November 12, 2016 5:36:37 PM PST, John <miaojpm at gmail.com> wrote:>Hi, > > We can match one numerical value as follows >> 3 %in% c(4,5) >[1] FALSE >> 3 %in% c(4,5,3) >[1] TRUE > > To see whether value pairs are identical, >> identical(c(3,4), c(3,5)) >[1] FALSE >> identical(c(3,4), c(3,4)) >[1] TRUE > > Is there any way to test whether ?A value pair is in a set of value >pairs?? For example, can we test whether the pair (2,3) is identical to >one >of the pairs in the set S={(1,2), (4,3), (3,3), (2,3), (4,5)}? > In this case, the answer is yes because the 4th element of S is (2,3). >Is there any simple way to code it? Thanks! > >John > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
> S <- list(c(1,2), c(4,3), c(3,3), c(2,3), c(4,5)) > list(c(1,2), c(3,4), c(2,3)) %in% S # is in S?[1] TRUE FALSE TRUE> match(list(c(1,2), c(3,4), c(2,3)), S) # which element of S?[1] 1 NA 4 Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Nov 12, 2016 at 5:36 PM, John <miaojpm at gmail.com> wrote:> Hi, > > We can match one numerical value as follows > > 3 %in% c(4,5) > [1] FALSE > > 3 %in% c(4,5,3) > [1] TRUE > > To see whether value pairs are identical, > > identical(c(3,4), c(3,5)) > [1] FALSE > > identical(c(3,4), c(3,4)) > [1] TRUE > > Is there any way to test whether ?A value pair is in a set of value > pairs?? For example, can we test whether the pair (2,3) is identical to one > of the pairs in the set S={(1,2), (4,3), (3,3), (2,3), (4,5)}? > In this case, the answer is yes because the 4th element of S is (2,3). > Is there any simple way to code it? Thanks! > > John > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.[[alternative HTML version deleted]]
Sorry, that was a fail. Better to think about:
any( 2 == p[,1] & 3 == p[,2] )
v <- matrix( c( 2,3, 1,2, 5,5 ), byrow=TRUE, ncol=2 )
apply( v, 1, function(x) { any( x[1]==p[,1] & x[2]==p[,2] ) } )
-- 
Sent from my phone. Please excuse my brevity.
On November 12, 2016 6:11:13 PM PST, Jeff Newmiller <jdnewmil at
dcn.davis.ca.us> wrote:>This really depends on the way you represent these pairs. Here is some
>food for thought:
>
>p <- matrix( c( 1,2, 4,3, 3,3, 2,3, 4,5 ), byrow=TRUE, ncol=2 )
>( 2 %in% p[,1] ) & ( 3 %in% p[,2] ) # (2,3)
>( c( 2, 1, 5 ) %in% p[,1] ) & ( c( 3, 2, 5 ) %in% p[,2] ) # (2,3) (1,2)
>(5,5)
>
>-- 
>Sent from my phone. Please excuse my brevity.
>
>On November 12, 2016 5:36:37 PM PST, John <miaojpm at gmail.com>
wrote:
>>Hi,
>>
>>   We can match one numerical value as follows
>>> 3 %in% c(4,5)
>>[1] FALSE
>>> 3 %in% c(4,5,3)
>>[1] TRUE
>>
>>   To see whether value pairs are identical,
>>> identical(c(3,4), c(3,5))
>>[1] FALSE
>>> identical(c(3,4), c(3,4))
>>[1] TRUE
>>
>>   Is there any way to test whether ?A value pair is in a set of value
>>pairs?? For example, can we test whether the pair (2,3) is identical
>to
>>one
>>of the pairs in the set S={(1,2), (4,3), (3,3), (2,3), (4,5)}?
>> In this case, the answer is yes because the 4th element of S is
>(2,3).
>>Is there any simple way to code it? Thanks!
>>
>>John
>>
>>	[[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>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.
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.