2014-12-16 8 views
0

내 프로필이 표시되면 내 모든 질문은 데이터 프레임에 있으며 여기에 또 다른 질문이 있습니다! R 데이터 프레임 : 날짜 범위 운영 - 특정 날짜 범위의 하위 행

나는 직불 및 신용 거래 간의 병합의 결과 특정 dataframe을

>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 

내 목표는 DebitDate가 5 CreditDate의 일 때문에 내가 시도 사이에 존재하는 행을 얻을 수 있습니다 I 위의 코드에서 : 연산자

FiveDays <- allTxns$CreditDate+5 #Results in a vector which has date + 5 days 

allTxns <- cbind(allTxns[1:2],FiveDays,allTxns[4:6]) #Adding the vector as a column of dataframe 

newDf <- allTxns[allTxns$DebitDate %in% allTxns$CreditDate:allTxns$FiveDays] 

를 사용 기간을 두는 데이터, 서브 세트, I는 상기 제 소자

를 사용하는 경우에만 다음 논리 오류를 얻고
Warning messages: 
    1: In mer32$DepositDate:mer32$FiveDays2 : 
     numerical expression has 3994 elements: only the first used 
    2: In mer32$DepositDate:mer32$FiveDays2 : 
     numerical expression has 3994 elements: only the first used 

따라서 필요한 출력은 첫 번째 Cust_no (12345)에만 적용되고 다른 행에는 적용되지 않습니다. 범위 조건이 모든 행에 적용되는지 어떻게 확인합니까 ??

잘못된 출력

>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 

>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 

답변

1

올바른 출력

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 
1

이 오래된 질문에 이미 허용 대답을했다보십시오. 그러나, 나는 그것이 그대로 질문과 답변이 간소화 될 수 있음에 유의하지 추가 FiveDays 열을 만드는 데 필요한 :

allTxns[with(allTxns, CreditDate <= DebitDate & DebitDate <= CreditDate + 5L), ] 
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 
관련 문제