logical
is a vector, I presume a logical one containing len
values TRUE
. x
is a vector of some other data of the same length.
The second part x != x[i]
is creating a logical vector with TRUE
where elements of x
aren't the same as the current value of x
for this iteration, and FALSE
otherwise.
As a result, both sides of &
are now logical vector. &
is an element-wise AND comparison the result of this is TRUE
if elements of logical
and x != x[i]
are both TRUE
and FALSE
otherwise. Hence, after the first iteration, logical
gets changed to a logical vector with TRUE
for all elements x
not the same as the i=1
th element of x
, and FALSE
if they are the same.
Here is a bit of an example:
logical <- rep(TRUE, 10)
set.seed(1)
x <- sample(letters[1:4], 10, replace = TRUE)
> x
[1] "b" "b" "c" "d" "a" "d" "d" "c" "c" "a"
> logical
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> x != x[1]
[1] FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> logical & x != x[1]
[1] FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
This seems very complex. Do you get the same results as:
unique(x)
gives you? If I run my x
above through myUniq()
and unique()
I get the same output:
> myUniq(x)
[1] "b" "d" "c" "a"
> unique(x)
[1] "b" "c" "d" "a"
(well, except for the ordering...)