[R] Create sequential vector for values in another column

Berend Hasselman bhh at xs4all.nl
Fri Oct 11 15:58:03 CEST 2013


On 11-10-2013, at 15:26, Steven Ranney <steven.ranney at gmail.com> wrote:

> Hello all -
> 
> I have an example column in a dataFrame
> 
> id.name
> 123.45
> 123.45
> 123.45
> 123.45
> 234.56
> 234.56
> 234.56
> 234.56
> 234.56
> 234.56
> 234.56
> 345.67
> 345.67
> 345.67
> 456.78
> 456.78
> 456.78
> 456.78
> 456.78
> 456.78
> 456.78
> 456.78
> 456.78
> ...
> [truncated]
> 
> And I'd like to create a second vector of sequential values (i.e., 1:N) for
> each unique id.name value.  In other words, I need
> 
> id.name  x
> 123.45   1
> 123.45   2
> 123.45   3
> 123.45   4
> 234.56   1
> 234.56   2
> 234.56   3
> 234.56   4
> 234.56   5
> 234.56   6
> 234.56   7
> 345.67   1
> 345.67   2
> 345.67   3
> 456.78   1
> 456.78   2
> 456.78   3
> 456.78   4
> 456.78   5
> 456.78   6
> 456.78   7
> 456.78   8
> 456.78   9
> 
> The number of unique id.name values is different; for some values, nrow()
> may be 42 and for others it may be 36, etc.
> 
> The only way I could think of to do this is with two nested for loops.  I
> tried it but because this data set is so large (nrow = 112,679 with 2,161
> unique values of id.name), it took several hours to run.
> 
> Is there an easier way to create this vector?  I'd appreciate your thoughts.

I named your dataframe dat1.
You can also do this

unlist(sapply(rle(dat1$id.name)$lengths, function(k) 1:k ))

And as Petr told you: beware of FAQ 7.31

Berend



More information about the R-help mailing list