Julia equivalent to 'not in' in R


#1

What would be Julia code for the following R code:
a <- c(1:5)
b <- c(1:10)
c <- b[-a]

Thanks,

Patrik


In julia, how to realize a=b[-1:2] as in R without modifying b
#2

I’m not an R expert, but you want the elements of b which are NOT part of a as well, right?

One way would be:

c = Iterators.filter(x -> x ∉ a, b) (on lastest master, if you’re on 0.5 it would be c = filter(x -> x ∉ a, b)).


#3

Do you want setdiff(b, a) ?


#4

I frequently use b[setdiff(1:end, a)].


#5

Oh, has filter() left Base? Must remember that…


#6

As far as I understand, no. But it has moved into a module called Iterators within Base.


#7

NamedArrays implements a Not method that does what the minus index does in R. I don’t know why a very common ideom like that is not in Julia Base though?


#8

I made a PR for something like that in Base a long while back. Seems like a reasonable addition to me.


#9

It’s funny to see that the branch is apparently still alive.


#10

Thanks for your replies. setdiff() seems to be a work around, but that function is the same as %in% in R.