[R] Capture change in variable in R

Abhinaba Roy abhinabaroy09 at gmail.com
Thu May 8 18:49:21 CEST 2014


Hi R helpers,

I have a dataframe like

   ID                   Yr_Mnth AMT_PAID AMT_DUE    paidToDue
CS00000026A    201301      320.48      1904    0.168319328
CS00000026A    201302    4881.31    15708    0.310753119
CS00000026A    201303    7609.04    25585    0.297402384
CS00000026A    201304    9782.70    21896    0.446780234
CS00000026A    201305    6482.01    22015    0.294436066
CS00000026A    201306    5226.28    14280    0.365985994
CS00000026A    201307    9078.47    19040    0.476810399
CS00000026A    201308    7060.33    23800    0.296652521
CS00000026A    201309    7595.57    17136    0.443252218
CS00000026A    201310    5388.64    24752    0.217705236

The problem I am facing is to capture the change in 'paidToDue' which is
define as follows

Let 'm' be the value of 'Yr_Mnth' in the current row (except the 1st row)
and 'm-1' be that in the previous row

I am trying to add a column to the dataframe 'Change' which will have
values 'Improve','Deteriorate' and 'No change', which are defined as


if (AMT_PAID(m) != AMT_PAID(m-1)) & sign(paidToDue(m)-paidToDue(m-1)==1 &
abs(paidToDue(m)-paidToDue(m-1))>0.1 then 'Change' = 'Improve'

if (AMT_PAID(m) != AMT_PAID(m-1)) & sign(paidToDue(m)-paidToDue(m-1) == -1
& abs(paidToDue(m)-paidToDue(m-1)) > 0.1 then 'Change' = 'Deteriorate'

else 'Change' = 'No change'

Note: I have 5000 unique ID in the data and this has to be done for each ID
and the data is sorted by Yr_Mnth.

Please find attached the csv file for reference.

How can it be done in R?
-- 
Regards
Abhinaba Roy


More information about the R-help mailing list