[R] data prep question

Bill.Venables at csiro.au Bill.Venables at csiro.au
Sun Jan 16 12:48:46 CET 2011


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
> dat
   ID TIME OBS
1 001    0  23
2 001  200  11
3 001 1000  10
4 001 2300  22
5 003    0  45
6 003 1705  32
7 005    0  56
8 005  100  34
9 005  500  23
> 


________________________________________
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Matthew Strother [rstrothe at gmail.com]
Sent: 16 January 2011 07:26
To: r-help at r-project.org
Subject: [R] data prep question

I have a data set with several thousand observations across time, grouped by subject (example format below)

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
...

I would like to identify the first time for each subject, and then subtract this value from each subsequent time.  However, the number of observations per subject varies widely (from 1 to 20), and the intervals between times varies widely.   Is there a package that can help do this, or a loop that can be set up to evaluate ID, then calculate the values?  The outcome I would like is presented below.
ID              TIME    OBS
001             0               23
001             200             11
001             1000    10
001             2300    22
003             0               45
003             1705    32
005             0               56
005             100             34
005             500             23
...

Any help appreciated.
______________________________________________
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