0

Let's assume I have the following sample data:

c(rep("A",6),rep("B",8),rep("A",3),rep("B",5),rep("A",9))

and I want to recode all the different groups of "A" and "B" as different groups, so whenever there is a change from "A" to "B" it means is a new group, in order to have this kind of output just to be clear:

c(rep("A",6),rep("B",8),rep("C",3),rep("D",5),rep("E",9))

I can easily do it with for loops, I am wondering if there is there a way to do it without using for loops, as I cannot think of a possible way with apply functions..

Thank you

adaien
  • 1,932
  • 1
  • 12
  • 26
  • 1
    You can use `rleid(x)`. The function is from `library(data.table)` – Pierre L Feb 24 '16 at 15:28
  • 1
    Exactly what I was looking for. Thank you – adaien Feb 24 '16 at 15:40
  • Just one thing @PierreLafortune. Probably I'm missing when a question should be exactly marked as duplicate, but I could now that the question had the same answer if I didn't know about the existence of `rleid`? Thank you – adaien Apr 12 '16 at 11:51
  • It is hard to tell sometimes when you aren't sure what to look for. We will find the duplicate for you. All you have to do is make a try to find an answer and show what you've tried. Don't worry about anything else. – Pierre L Apr 12 '16 at 14:56
  • Your question has some unique elements to it so it would be hard for a beginner to search for generalized approaches. – Pierre L Apr 12 '16 at 15:04

0 Answers0