2012-08-13 2 views
2

편집 :이 문제는 오래된 버전의 data.table이 설치 되었기 때문에 발생했습니다.사용 : = with j with data.table (구현되지 않은 기능의 해결 방법)

나는 같은 data.table이 다음 :이처럼 보이도록, 컬러 당 누적 합계를 싶습니다

require(xts) 
a <- data.table(colour=c("Red","Green","Blue","Blue","Black","Black"), date=c(as.Date("2011-07-04"),as.Date("2011-07-10"),as.Date("2011-07-09"),as.Date("2011-07-12"),as.Date("2011-07-04"),as.Date("2011-07-09")),daily.quantity=c(1,-1,2,-2,1,1)) 

    colour  date daily.quantity 
[1,] Red 2011-07-04    1 
[2,] Green 2011-07-10    -1 
[3,] Blue 2011-07-09    2 
[4,] Blue 2011-07-12    -2 
[5,] Black 2011-07-04    1 
[6,] Black 2011-07-09    1 

:

 colour  date daily.quantity cumulative.quantity 
[1,] Black 2011-07-04    1     1 
[2,] Black 2011-07-09    1     2 
[3,] Blue 2011-07-09    2     2 
[4,] Blue 2011-07-12    -2     0 
[5,] Green 2011-07-10    -1     -1 
[6,] Red 2011-07-04    1     1 

을하지만, 내가하려고하면 다음, I 색을 고려하지 않은 누적 합계로 끝납니다.

setkey(a,colour,date) 
a[,cumulative.quantity := cumsum(daily.quantity)] 

    colour  date daily.quantity cumulative.quantity 
[1,] Black 2011-07-04    1     1 
[2,] Black 2011-07-09    1     2 
[3,] Blue 2011-07-09    2     4 
[4,] Blue 2011-07-12    -2     2 
[5,] Green 2011-07-10    -1     1 
[6,] Red 2011-07-04    1     2 

불행히도 구현되지 않음 :

> a[,cumulative.quantity := cumsum(daily.quantity),keyby="colour,date"] 
Error in `[.data.table`(a, , `:=`(cumulative.quantity, cumsum(daily.quantity)), : 
    Combining := in j with by is not yet implemented. Please let maintainer('data.table') know if you are interested in this. 

누구나 해결 방법을 제안 할 수 있습니까?

답변

2

by을 그룹은 colour에 있어야합니다 :

a[,cumulative.quantity := cumsum(daily.quantity), by=colour] 
    colour  date daily.quantity cumulative.quantity 
1: Black 2011-07-04    1     1 
2: Black 2011-07-09    1     2 
3: Blue 2011-07-09    2     2 
4: Blue 2011-07-12    -2     0 
5: Green 2011-07-10    -1     -1 
6: Red 2011-07-04    1     1 
+0

는이 코드를 실행합니까 나는 위의 '구현되지 않은'오류가 발생 – Ina

+0

예,이 코드는 잘 작동 @Ina 나는 데이터 '의 버전 1.8.2이... 테이블' . – Andrie

+0

고맙습니다. 제 버전을 업그레이드해야했습니다. 하지만 당신 말이 맞아요, 둘 다 색보다는 오히려 그룹화해야합니다 - 만약 내가 오류를 치고 아니었다면 내가 눈치 챘을 바랍니다 :) – Ina

3

'날짜'및 '색상'으로 합계를 원하지 않으며 '색상'만 사용해야합니다. data.table은 pkg : data.table에 있기 때문에 왜 xts가 필요한지 잘 모르겠습니다.

> a[,cumulative.quantity := cumsum(daily.quantity), by=c("colour") ] 
    colour  date daily.quantity cumulative.quantity 
1: Black 2011-07-04    1     1 
2: Black 2011-07-09    1     2 
3: Blue 2011-07-09    2     2 
4: Blue 2011-07-12    -2     0 
5: Green 2011-07-10    -1     -1 
6: Red 2011-07-04    1     1 

실제로는 것을 의미하는 것 두 개의 열 (가 wnat 않은 경우 "원하는 - 투 -보기 --이 같은"예를 잘못이다, 당신이 할 수있는 :

> setkey(a,colour,date) 
> a[,cumulative.quantity := cumsum(daily.quantity), by=c("colour", "date") ] 
    colour  date daily.quantity cumulative.quantity 
1: Black 2011-07-04    1     1 
2: Black 2011-07-09    1     1 
3: Blue 2011-07-09    2     2 
4: Blue 2011-07-12    -2     -2 
5: Green 2011-07-10    -1     -1 
6: Red 2011-07-04    1     1