%in% operator - NOT IN

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

%in% operator - NOT IN

Dan Abner
Hello everyone,

I am attempting to use the %in% operator with the ! to produce a NOT IN type
of operation. Why does this not work? Suggestions?

> data2[data1$char1 %in% c("string1","string2"),1]<-min(data1$x1)
> data2[data1$char1 ! %in% c("string1","string2"),1]<-max(data1$x1)+1000

Error: unexpected '!' in "data2[data1$char1 !"


Thanks!

Dan

XXXX

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: %in% operator - NOT IN

Berwin A Turlach-3
G'day Dan,

On Sun, 8 May 2011 05:06:27 -0400
Dan Abner <[hidden email]> wrote:

> Hello everyone,
>
> I am attempting to use the %in% operator with the ! to produce a NOT
> IN type of operation. Why does this not work? Suggestions?
>
> > data2[data1$char1 %in% c("string1","string2"),1]<-min(data1$x1)
> > data2[data1$char1 ! %in%
> > c("string1","string2"),1]<-max(data1$x1)+1000
>
> Error: unexpected '!' in "data2[data1$char1 !"

Try (untested)

R> data2[!(data1$char1 %in% c("string1","string2")),1]<-max(data1$x1)+1000

HTH.

Cheers,

        Berwin

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: %in% operator - NOT IN

baptiste auguie-5
Hi,

On 8 May 2011 21:18, Berwin A Turlach <[hidden email]> wrote:
> G'day Dan,
>
> On Sun, 8 May 2011 05:06:27 -0400
> Dan Abner <[hidden email]> wrote:
>
>> Hello everyone,
>>
>> I am attempting to use the %in% operator with the ! to produce a NOT
>> IN type of operation. Why does this not work? Suggestions?

Alternatively,

example(`%in%`)

or

`%ni%` = Negate(`%in%`)

HTH,

baptiste

>>
>> > data2[data1$char1 %in% c("string1","string2"),1]<-min(data1$x1)
>> > data2[data1$char1 ! %in%
>> > c("string1","string2"),1]<-max(data1$x1)+1000
>>
>> Error: unexpected '!' in "data2[data1$char1 !"
>
> Try (untested)
>
> R> data2[!(data1$char1 %in% c("string1","string2")),1]<-max(data1$x1)+1000
>
> HTH.
>
> Cheers,
>
>        Berwin
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: %in% operator - NOT IN

Ted Harding
In reply to this post by Berwin A Turlach-3
On 08-May-11 09:18:55, Berwin A Turlach wrote:

> G'day Dan,
>
> On Sun, 8 May 2011 05:06:27 -0400
> Dan Abner <[hidden email]> wrote:
>
>> Hello everyone,
>> I am attempting to use the %in% operator with the ! to produce
>> a NOT IN type of operation. Why does this not work? Suggestions?
>>
>> > data2[data1$char1 %in% c("string1","string2"),1]<-min(data1$x1)
>> > data2[data1$char1 ! %in%
>> > c("string1","string2"),1]<-max(data1$x1)+1000
>>
>> Error: unexpected '!' in "data2[data1$char1 !"
>
> Try (untested)
>
> R> data2[!(data1$char1 %in%
> c("string1","string2")),1]<-max(data1$x1)+1000
>
> HTH.
> Cheers,
>       Berwin

Berwin's suggestion should work -- it is the general way to
negate the result of an "%in".

As to "Why does this not work?", the point to note is that
"%in%" is a binary operator. If you enter
  ?"%in%"
you will be taken to the help page for "match", where it is
pointed out that:

  ?%in%? is a more intuitive interface as a binary operator,
  which returns a logical vector indicating if there is a
  match or not for its left operand.

Specifically, therefore, the syntax of "%in%" requires

  X %in% Y

where X and Y are objects to which the functional definition
of %in% applies (see the same help page):

  '%in%' is currently defined as
  '"%in%" <- function(x, table) match(x, table, nomatch = 0) > 0'

In your expression (effectively "X ! %in% Y") the item which
immediately precedes "%in%" is the "!", and this is not a
valid item!

Based on the above functional definition, you could define
your own binary operator "%!in%" as

"%!in%" <- function(x,table) match(x,table, nomatch = 0) == 0

or similar -- I have not tested this so cannot guarantee it!
However, it is the way to proceed if you want a "NOT IN".
Then the usage could be:

data2[data1$char1 %!in% c("string1","string2"),1]<-max(data1$x1)+1000

Hoping ths helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 08-May-11                                       Time: 10:35:05
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: %in% operator - NOT IN

Duncan Mackay-2
In reply to this post by Dan Abner

At 19:06 08/05/2011, you wrote:

>Hello everyone,
>
>I am attempting to use the %in% operator with the ! to produce a NOT IN type
>of operation. Why does this not work? Suggestions?
>
> > data2[data1$char1 %in% c("string1","string2"),1]<-min(data1$x1)
> > data2[data1$char1 ! %in% c("string1","string2"),1]<-max(data1$x1)+1000
>
>Error: unexpected '!' in "data2[data1$char1 !"
>
>
>Thanks!
>
>Dan
>
>XXXX
>
>         [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.

Hi Dan

See the last of the examples for ?match

"%w/o%" <- function(x, y) x[!x %in% y] #--  x without y
(1:10) %w/o% c(3,7,12)

I think it was Peter Dalgaard who pointed this out some years ago

HTH

Regards


Duncan Mackay
Department of Agronomy and Soil Science
University of New England
ARMIDALE NSW 2351
Email: home [hidden email]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: %in% operator - NOT IN

Karl Ove Hufthammer
In reply to this post by Dan Abner
Dan Abner wrote:

> I am attempting to use the %in% operator with the ! to produce a NOT IN
> type of operation.

Just use the ‘%nin‰’ operator in the ‘Hmisc’ package. :-)

--
Karl Ove Hufthammer

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