R: compare the next two values in a vector with each other (without looping if possible) -


i have vector this:

10  7  7 10  7 10  7 10 10  7 10 10  7  7 10 10  7 10  7  7 10  7 10 

i want compare entries of vector in pairs: e.g. first entry second, third fourth until in pair have 2 equal entries. in example 2 equal values occur first time in sixth pair or in other words 11th , 12th values equal. important want have index of 11th row , continue comparison between 12th , 13th row.

is there way (i prefer without looping)?

edit: didn't explain myself clear enough. when pair has equal values delete first entry of these 2 values. indices of pairs not known start. in above example, desired output be:

10  7  7 10  7 10  7 10 10  7 10  7  7 10 10  7 10  7  7 10  7 10 

and index of row has been deleted:

11 

in case 1 row had deleted pairs consist of 7 , 10.

based on edited version of question, it's clear need sort of looping function, because decisions on previous indices affect decisions on subsequent indices. efficient way can think populate logical vector indicating whether each index should kept in vector. afterward can use logical vector both remaining values , indices removed.

x <- c(10,  7,  7, 10,  7, 10,  7, 10, 10,  7, 10, 10,  7,  7, 10, 10, 7, 10,  7,  7, 10,  7, 10) keep <- rep(true, length(x)) <- true (pos in 2:length(x)) {   if (even & x[pos] == x[pos-1]) {     keep[pos-1] <- false   } else {     <- !even   } } x[keep] # [1] 10  7  7 10  7 10  7 10 10  7 10  7  7 10 10  7 10  7  7 10  7 10 which(!keep) # [1] 11 

as looping function, rcpp can used speedup:

library(rcpp) cppfunction( "logicalvector getbin(numericvector x) {   const int n = x.size();   logicalvector keep(n, true);   bool = true;   (int pos=1; pos < n; ++pos) {     if (even && x[pos] == x[pos-1]) {       keep[pos-1] = false;     } else {       = !even;     }   }   return keep; }") 

benchmarking of pure-r , rcpp approaches:

# larger dataset set.seed(144) x <- sample(1:10, 1000, replace=t)  # functions compare purer <- function(x) {   keep <- rep(true, length(x))   <- true   (pos in 2:length(x)) {     if (even & x[pos] == x[pos-1]) {       keep[pos-1] <- false     } else {       <- !even     }   }   list(x[keep], which(!keep)) } with.rcpp <- function(x) {   keep <- getbin(x)   list(x[keep], which(!keep)) } all.equal(purer(x), with.rcpp(x)) # [1] true library(microbenchmark) microbenchmark(purer(x), with.rcpp(x)) # unit: microseconds #          expr     min       lq       mean   median       uq       max neval #      purer(x) 855.318 1066.177 1806.67855 1140.656 1442.869 35379.369   100 #  with.rcpp(x)  30.137   62.304   86.80656   78.132   94.771   348.598   100 

with vector of length 1000 see speedup of more 10x using rcpp. speedup relevant larger vectors.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -