[R] Add sequence numbers to lines with the same ID: How can this be accomplished?

Rolf Turner r.turner at auckland.ac.nz
Sun Oct 25 01:02:48 CEST 2015


On 25/10/15 11:28, John Sorkin wrote:
> I have a file that has (1) Line numbers, (2) IDs. A given ID number can appear in more than one row. For each row with a repeated ID, I want to add a number that gives the sequence number of the repeated ID number. The R code below demonstrates what I want to have, without any attempt to produce the result, as I have no idea how to accomplish my goal.
>
>
> line <- c(1,2,3,4,5,6,7,8,9,10)
> ID<-    c(1,1,2,3,4,5,6,7,8,8)
> cat("Note lines 1 and 2 both contain ID 1; lines 9 and 10 both contain ID 8")
> cbind(line,ID)
> Seq <-  c(1,2,1,1,1,1,1,1,1,2)
> cat("Sequence numbers within ID added to the data")
> cbind(line,ID,Seq)

I *think* that

   unlist(lapply(rle(ID)$lengths,seq_len))

gives what you want.  At least it does for the given example.

cheers,

Rolf Turner

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list