[R] Computing row differences in new columns

David Winsemius dwinsemius at comcast.net
Mon Mar 21 21:10:12 CET 2011


On Mar 21, 2011, at 3:38 PM, Roberto Lodeiro Muller wrote:

>
> -----Original Message-----
> From: Roberto Lodeiro Muller <roberto.muller at doctor.com>
> To: roberto.muller at doctor.com
> Sent: Mon, Mar 21, 2011 3:37 pm
> Subject: Re: [R] Computing row differences in new columns
>
>
> Sorry, my data appeared badly formatted to me, so I converted it to  
> plain text:
>
> And just to clarify, for each subject in the first row it should  
> appear the difference to the next row, so that the last entry on  
> each subject would be a NA.
>
> Thanks again for your help
>
> Roberto
>
> SUBJECT    DATE       RESULT     DateDiff   ResultDiff
> 10751      22-Jul-03  3.5
> 10751      13-Feb-04  1.3
> 10751      20-Aug-04  1.6
> 10751      08-Mar-05  1.7
> 10751      30-Aug-05  1.6
> 10751      21-Feb-06  1.3
> 10751      31-Aug-06  1.2
> 10751      27-Feb-07  1.5
> 10751      29-Aug-07  1
> 10752      29-Jul-03  5.9
> 10752      24-Feb-04  5
> 10752      25-Aug-04  3.6
> 10752      11-Mar-05  5.1
> 10752      18-Sep-05  2.2
> 10752      23-Feb-06  3.1
> 10752      24-Aug-06  3.7
> 10752      27-Feb-07  6

dat$dt <- as.Date(dat$DATE, format="%d-%b-%y")
dat$diffdt <- c(diff(dat$dt), NA)
dat$diffRES <- c(diff(dat$RES), NA)


-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list