Reputation: 55
I have a vector of the type
c(3,3,...,9,9,...,2,2,...,3,3,...,7,7,...)
I want to remove the repeated numbers in a sequence, without breaking the order. This is, I would like to obtain something like
c(3,9,2,3,7,...)
How can I do this in R?
Upvotes: 4
Views: 602
Reputation: 23798
Just for the fun of it, here is an Rcpp
version of solving the problem:
library(Rcpp)
cppFunction('NumericVector remove_multiples(NumericVector& vec) {
NumericVector c_vec(clone(vec));
NumericVector::iterator it = std::unique(c_vec.begin(),c_vec.end());
c_vec.erase(it,c_vec.end());
return(c_vec);
}'
)
x <- c(1,1,1,2,2,2,1,1,3,4,4,1,1)
> remove_multiples(x)
[1] 1 2 1 3 4 1
Upvotes: 2
Reputation: 13149
We can also use the observation that a duplicate in sequence has a difference of 0 with their neighbour. Therefore, using base-R, we can do:
v[c(1,diff(v))!=0]
Upvotes: 9
Reputation: 887251
We can try with rleid
and duplicated
. We create the run-length-ids with rleid
(from data.table
) so that only adjacent elements that are equal forms one group, get the logical index of not duplicated
values and subset the vector.
library(data.table)
v1[!duplicated(rleid(v1))]
#[1] 3 9 2 3 7
Or as the OP mentioned, we can use rle
from base R
and extract the values
.
rle(v1)$values
#[1] 3 9 2 3 7
v1 <- c(3,3,9,9,2,2,3,3,7,7)
Upvotes: 7