Defining row indices for members of a .table data group

The data.table package in R provides the ability to:

which: 'TRUE returns the number of integer strings' x that' i matches.

However, I see no way to get within the jintegers of the 'x' strings within the groups set with by.

For example, this ...

DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6))

... I would like to know the indices in DT for each y value.

The meaning for me is that I use the data.table in parallel with another data structure (ADS), to which I intend to perform group calculations based on effectively calculated data table groupings.

For example, if ADS is a vector with a value for each row in the DT:

ADS<-sample(100,nrow(DT))

ADS, DT $y , .

DT[,seqNum:=seq_len(nrow(DT))]
DT[,mean(ADS[seqNum]),by=y]

, .

, , tapply:

tapply(ADS,DT$y,mean)

, data.tables( "by" ).

, - , ???

, data.table, (, )???

: '.which' , :

DT[,mean(ADS[.which]),by=y,which=TRUE]
+5
2

data.table 1.8.3, .I j data.table, ...

DT[ , list( yidx = list(.I) ) , by = y ]
#   y  yidx
#1: 1 1,4,7
#2: 3 2,5,8
#3: 6 3,6,9
+10

, . .N :

DT <- data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6))
setkey(DT, y)

ii <- DT[,.N, by=y]
ii[, start := cumsum(N) - N[1] + 1][,end := cumsum(N)][, N := NULL]
#    y start end
# 1: 1     1   3
# 2: 3     4   6
# 3: 6     7   9

( , seqNum. , , , , .)

+6

All Articles