[R] recode the ID with sequential order of a dataset

arun smartpink111 at yahoo.com
Sat May 24 04:50:01 CEST 2014


Hi,
May be this helps:
#if the data is ordered for the "TIME" column as in the example

dat <- read.table(text="ID TIME 
1254 0
1254 1
1254 3
1254 5
1254 14
3236 0
3236 36
3236 93
1598 0
1598 0.5
1598 1
1598 2
1598 3
1598 12
1598 36
1598 75
1598 95
1598 120",sep="",header=TRUE)

 dat$NewID <- with(dat, ave(rep(1,nrow(dat)),ID,FUN=cumsum))

#if your dataset is not ordered
dat1 <- structure(list(ID = c(1254L, 1254L, 1254L, 1254L, 1254L, 3236L, 
3236L, 3236L, 1598L, 1598L, 1598L, 1598L, 1598L, 1598L, 1598L, 
1598L, 1598L, 1598L), TIME = c(0, 1, 3, 5, 14, 0, 93, 36, 0, 
0.5, 1, 2, 3, 12, 36, 95, 75, 120)), .Names = c("ID", "TIME"), class = "data.frame", row.names = c(NA, 
-18L))

dat1$NewID <- with(dat1,ave(TIME,ID,FUN=order))

A.K.


 hi all,


I have the following dataset:
ID TIME DV
1254 0 .
1254 1 .
1254 3 .
1254 5 .
1254 14 .
3236 0 .
3236 36 .
3236 93 .
1598 0 .
1598 0.5 .
1598 1 .
1598 2 .
1598 3 .
1598 12 .
1598 36 .
1598 75 .
1598 95 .
1598 120 .
. . .
. . .
. . .

I want to generate a new ID column with the ID sequentially like 1,2,3,...... It would be easy to do it with equal time points for each individuals, however, right now, the data have different time points for different individuals, how can I do this to be able get the dataset I wanted?

Thanks,

York 




More information about the R-help mailing list