[R] Computing row differences in new columns
Alexander Engelhardt
alex at chaotic-neutral.de
Mon Mar 21 21:02:55 CET 2011
(mods: can you un-flag me if I am still flagged?)
Hi,
First, I would convert the DATE column to an integer. I found this by
quick googling, perhaps as.Date is your weapon of choice:
https://stat.ethz.ch/pipermail/r-help/2008-August/169870.html
For the column generation I think you will have to use a for-loop here:
yourdata$ResultDiff <- 0
yourdata$DateDiff <- 0
for( i in 1:nrow(yourdata) ){
yourdata$ResultDiff[i] <- yourdata$RESULT[i] - yourdata$RESULT[i+1] #
should automatically NA the last cell
yourdata$DateDiff[i] <- yourdata$datenumber[i] - yourdata$datenumber[i+1]
}
where datenumber is your integer column of the date.
-- Alex
Am 21.03.2011 20:38, schrieb Roberto Lodeiro Muller:
>
> -----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
More information about the R-help
mailing list