data.table . , ( ) .
library(data.table)
source <-
data.table(data.frame(key = c("A","B","C","D","A","B","A","B","B","B", "C"),
value = c(1,1,1,1,4,4,23,23,26,26,30)))
data.table(data.frame(..., data.table() key. data.table "key". , , :
source <- data.table(Key = c("A","B","C","D","A","B","A","B","B","B","C"),
Value = c(1,1,1,1,4,4,23,23,26,26,30))
, as.integer() , Value numeric integer. , 1 numeric R, 1L, integer. integer , integer, integer numeric. , L .
source[,Value:=as.integer(Value)]
distance <- 22L
setkey(source, Key, Value)
# Heart of the solution (following a few explanatory comments):
# "J()" : shorthand for 'data.table()'
# ".N" : returns the number of rows that matched a line (see ?data.table)
# "[[3]]" : as with simple data.frames, extracts the vector in column 3
source[,count:=source[J(Key,Value+distance),.N][[3]]]
source
key value count
[1,] A 1 1
[2,] A 4 0
[3,] A 23 0
[4,] B 1 1
[5,] B 4 2
[6,] B 23 0
[7,] B 26 0
[8,] B 26 0
[9,] C 1 0
[10,] C 30 0
[11,] D 1 0
, := source , . setkey() . , :
source <- data.table(Key = c("A","B","C","D","A","B","A","B","B","B","C"),
Value = c(1,1,1,1,4,4,23,23,26,26,30))
source[,Value:=as.integer(Value)]
source[,count:=setkey(copy(source))[source[,list(Key,Value+distance)],.N][[3]]]
Key Value count
[1,] A 1 1
[2,] B 1 1
[3,] C 1 0
[4,] D 1 0
[5,] A 4 0
[6,] B 4 2
[7,] A 23 0
[8,] B 23 0
[9,] B 26 0
[10,] B 26 0
[11,] C 30 0