[R] Create sequential vector for values in another column
arun
smartpink111 at yahoo.com
Fri Oct 11 15:43:28 CEST 2013
Also,
it might be faster to use ?data.table()
library(data.table)
dt1<- data.table(dat1,key='id.name')
dt1[,x:=seq(.N),by='id.name']
A.K.
On , arun <smartpink111 at yahoo.com> wrote:
Hi,
Try:
dat1<-
structure(list(id.name = c(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)), .Names = "id.name", class = "data.frame", row.names = c(NA,
-23L))
dat1$x <- with(dat1,ave(id.name,id.name,FUN=seq))
A.K.
On Friday, October 11, 2013 9:28 AM, 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.
Thanks -
SR
Steven H. Ranney
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list