19389

Remove duplicates in vector to next value

Question:

I have a vector consisting of integers which can take a finite number of values. Consider the case where the values is 0,1,2.

a<-c(0,0,0,1,1,0,0,2,2,2)

I want to delete replicates of values until the next differnet value. After doing this the result show be

0 1 0 2

Hence I want to do something like unique (which would have given 0,1,2) but i want to take the ordering into account.

Thanks

Answer1:

try (assuming no NAs):

a[c(1, diff(a)) != 0]

Answer2:

Use my favorite tool:

Rgames> rle(a<-c(0,0,0,1,1,0,0,2,2,2)) Run Length Encoding lengths: int [1:4] 3 2 2 3 values : num [1:4] 0 1 0 2

Recommend

  • Remove duplicates in vector to next value
  • Remove duplicates in vector to next value
  • Full width list in CSS
  • Best way to localize fonts in wpf
  • Whats the diff between cdnjs vs npmjs
  • Prevent WCF from encoding strings in response?
  • Efficient way to compute multiple euclidean distances Matlab
  • Shorten a text string in PHP
  • Find “command not found” when executed in bash loop
  • Finding an index of a max value of a list in F#
  • Docker in Google App Engine Flexible health check has wrong URL
  • Is there an overview of all codes that can be used inside NSLog()?
  • javascript number literal using exponent
  • Three-way xor-like function
  • Concat in an empty array in Javascript
  • how to calculate shannon entropy of byte bigrams
  • Twitter Bootstrap - Drop down menu - Arrows Keys are not working for input tags in firefox
  • What is the Best method to access hiberante search (lucene) files on amazon s3
  • Javascript to Excel: bad performance for lack of “range” method
  • Can't get LogCat (Alcatel OneTouch Evolve)
  • Defining variable by logical subseting on time interval in data.table
  • MATLAB - Classification output
  • SQL append distinct values from two columns and two tables
  • Hibernate: Inheritance and relationship mapping + generics
  • Partial specialization of a class template in derived class affects base class
  • Should I use composite primary keys in Grails?
  • Adding a button at the bottom of a table view
  • Getting last autonumber in access
  • Sending data from AppleScript to FileMaker records
  • Delete MySQLi record without showing the id in the URL
  • GridView Sorting works once only
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal