datetime - R Dataframe : Date range Operations - Subset those rows which falls under a certain date range -
if see profile, questions on dataframes , here another!
i have dataframe result of merge between debit , credit transactions
>head(alltxns) cust_no creditdate credit debitdate debit 1 12345 2014-10-01 200 2014-10-03 400 2 12345 2014-10-01 200 2014-10-04 150 3 12345 2014-10-01 200 2014-10-15 800 4 33344 2014-10-03 500 2014-10-04 50 5 33344 2014-10-03 500 2014-10-05 504 6 33344 2014-10-03 500 2014-10-06 332 7 33344 2014-10-03 500 2014-10-08 56 8 66554 2014-10-10 660 2014-10-04 150 9 66554 2014-10-10 660 2014-10-05 800 10 66554 2014-10-10 660 2014-10-11 400 11 66554 2014-10-10 660 2014-10-12 150 12 66554 2014-10-10 660 2014-10-13 800
my aim rows, debitdate lies between 5 days of creditdate , hence tried subset data, put date range using :
operator
fivedays <- alltxns$creditdate+5 #results in vector has date + 5 days alltxns <- cbind(alltxns[1:2],fivedays,alltxns[4:6]) #adding vector column of dataframe newdf <- alltxns[alltxns$debitdate %in% alltxns$creditdate:alltxns$fivedays]
in above code, i'm getting following logical error first element being used
warning messages: 1: in mer32$depositdate:mer32$fivedays2 : numerical expression has 3994 elements: first used 2: in mer32$depositdate:mer32$fivedays2 : numerical expression has 3994 elements: first used
hence required output getting limited first cust_no (12345) , not being applied other rows. how make sure range condition gets applied rows??
incorrect output
>head(newdf) row.names cust_no creditdate credit debitdate debit 1 12345 2014-10-01 200 2014-10-03 400 2 12345 2014-10-01 200 2014-10-04 150 4 33344 2014-10-03 500 2014-10-04 50 5 33344 2014-10-03 500 2014-10-05 504 6 33344 2014-10-03 500 2014-10-06 332 7 33344 2014-10-03 500 2014-10-08 56 8 66554 2014-10-10 660 2014-10-04 150 9 66554 2014-10-10 660 2014-10-05 800 10 66554 2014-10-10 660 2014-10-11 400 11 66554 2014-10-10 660 2014-10-12 150 12 66554 2014-10-10 660 2014-10-13 800
correct output
>head(newdf) row.names cust_no creditdate credit debitdate debit 1 12345 2014-10-01 200 2014-10-03 400 2 12345 2014-10-01 200 2014-10-04 150 4 33344 2014-10-03 500 2014-10-04 50 5 33344 2014-10-03 500 2014-10-05 504 6 33344 2014-10-03 500 2014-10-06 332 7 33344 2014-10-03 500 2014-10-08 56 10 66554 2014-10-10 660 2014-10-11 400 11 66554 2014-10-10 660 2014-10-12 150 12 66554 2014-10-10 660 2014-10-13 800
try
alltxns[with(alltxns , creditdate < debitdate & debitdate <=fivedays),] # cust_no creditdate fivedays credit debitdate debit #1 12345 2014-10-01 2014-10-06 200 2014-10-03 400 #2 12345 2014-10-01 2014-10-06 200 2014-10-04 150 #4 33344 2014-10-03 2014-10-08 500 2014-10-04 50 #5 33344 2014-10-03 2014-10-08 500 2014-10-05 504 #6 33344 2014-10-03 2014-10-08 500 2014-10-06 332 #7 33344 2014-10-03 2014-10-08 500 2014-10-08 56 #10 66554 2014-10-10 2014-10-15 660 2014-10-11 400 #11 66554 2014-10-10 2014-10-15 660 2014-10-12 150 #12 66554 2014-10-10 2014-10-15 660 2014-10-13 800
Comments
Post a Comment