I've set multiple keys in a data.table, but when I try and select rows by multiple values of the keys, it seems to return a row for each potential combination, but filled with NA for rows that do not exist.
I can get the sample code in 1c of this document, so it must be something that I'm just not seeing. Any help would be much appreciated.
library(data.table)
dt = data.table(colA = 1:4,
colB = c("A","A","B","B"),
colC = 11:14)
setkey(dt,colA,colB)
print(dt)
# colA colB colC
# 1: 1 A 11
# 2: 2 A 12
# 3: 3 B 13
# 4: 4 B 14
print(
dt[.(2,"A")]
)
# As expected
# colA colB colC
# 1: 2 A 12
print(
dt[.(c(2,3),"A")]
)
# colA colB colC
# 1: 2 A 12
# 2: 3 A NA #Unexpected
print(
dt[.(unique(colA),"A")]
)
# colA colB colC
# 1: 1 A 11
# 2: 2 A 12
# 3: 3 A NA #Unexpected
# 4: 4 A NA #Unexpected