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

arun smartpink111 at yahoo.com
Wed Jun 4 20:34:36 CEST 2014


Hi York,
Using "dat" (assuming that the data is ordered by "ID")
dat$NEWID <- cumsum(c(TRUE,with(dat, ID[-1]!= ID[-length(ID)])))

A.K.




Hi arun,

Thanks for your reply. I think you misunderstood my question, for the new ID, I would like to have the following pattern,
ID     TIME NEWID
1254 0      1
1254 1      1
1254 3      1
1254 5      1
1254 14     1
3236 0      2
3236 36    2
3236 93    2
1598 0      3
1598 0.5    3
1598 1       3
1598 2      3
1598 3      3
1598 12    3
1598 36    3
1598 75    3
1598 95    3
1598 120  3


How can I do this? Thanks again for your help.

York 



On Friday, May 23, 2014 10:50 PM, arun <smartpink111 at yahoo.com> wrote:
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