0

I would like to give a sequence of numbers to a new column to a data frame. But this sequence will repeat several times based on a value in another column. (i.e It starts from 1 until that specific value will be changed to other value).

My problem is how to define the ending point for each sequence in r.

A part of my data frame with the column "V2" which I intend to add:

  V1      V2(new added column with sequential numbers)           
  12      1
  12      2
  12      3
  12      4
  12      5
  13      1
  13      2
  13      3
  13      4
  13      5
  13      6
  14      1
  14      2
  14      3
  14      4

I tried to use the following code, which was not working!

count <- table(df$V1)
c <- as.integer(names(count)[df$V1==12])
    repeat{
      df$V2<- seq(1,c, by=1)
      if(df$V1!=12){
        break
      }
    }
MASUMEH
  • 41
  • 6

3 Answers3

1

It sounds like you might be looking for rle since you're interested in any time the "V1" variable changes.

Try the following:

> sequence(rle(df$V1)$lengths)
 [1] 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • Thanks a lot.Interestingly, it works. I'm on cloud nine right now!Because it took a lot of my time to solve this problem! – MASUMEH Apr 11 '14 at 09:06
0

Well Ananda beats my effort:

vec = numeric(0)
for(i in unique(df$V1)){
  n = length(df$V1[df$V1 == i])
  vec = c(vec, 1:n)
}
geotheory
  • 22,624
  • 29
  • 119
  • 196
0

rle is a very good solution but you could also have used ave:

tab$V2 <- ave(tab$V1, tab$V1, FUN=seq_along)

hth

droopy
  • 2,788
  • 1
  • 14
  • 12
  • There is a difference between `ave` and `rle` (and a reason that I did not suggest `ave`). Their question mentions *changes* from one value to another. This will generate a running sequence, even with breaks in the sequences. – A5C1D2H2I1M1N2O1R2T1 Apr 11 '14 at 09:43
  • yes it is true that it would work only if the numbers are consecutive – droopy Apr 11 '14 at 12:19