[R] data prep question

Hadley Wickham hadley at rice.edu
Sun Jan 16 18:47:13 CET 2011


On Sun, Jan 16, 2011 at 5:48 AM,  <Bill.Venables at csiro.au> wrote:
> Here is one way
>
> Here is one way:
>
>> con <- textConnection("
> + ID              TIME    OBS
> + 001             2200    23
> + 001             2400    11
> + 001             3200    10
> + 001             4500    22
> + 003             3900     45
> + 003             5605     32
> + 005             1800    56
> + 005             1900    34
> + 005             2300    23")
>> dat <- read.table(con, header = TRUE,
> + colClasses = c("factor", "numeric", "numeric"))
>> closeAllConnections()
>>
>> tmp <- lapply(split(dat, dat$ID),
> + function(x) within(x, TIME <- TIME - min(TIME)))
>> split(dat, dat$ID) <- tmp

Or, in one line with ddply:

library(plyr)
ddply(dat, "ID", transform, TIME = TIME - min(TIME))

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list