0

Some simple sample data:

test <- c(rep('B', 10), rep('A', 7), rep('C', 10), rep('A', 3))

#1] "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "A" "A" "A" "A" "A" "A" "A" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "A" "A" "A"

I would like to assign a numeric variable to this where the first block of 'B' gets a 1, the first block of 'A' gets a 2, the first block of 'C' gets a 3, and the next block of 'A' gets a 4. I tried:

test <- factor(test, levels = unique(test))
as.integer(test)

but that assigns that second block of 'A' a 2. How can I get it to assign unique, sequential numbers for each block? the actual data is drug combinations and I need the assigned numeric variable to start with 1

1 Answers1

4

I guess you need rle

> with(rle(test), rep(seq_along(lengths), lengths))
 [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81