I have a table with two numeric rows, one of which is set to key. I would like to subset my data.table by numeric key value, but it doesn’t seem to work. When I convert it to character, it works.
Could you help me to understand why is that? I am using data.table 1.8.6.
Thanks a bunch. Here is the test code:
> ID <-c(rep(210, 9), rep(3917,6))
> Count <- c(1,1,0,1,1,1,1,1,1,1,1,1,1,0,1)
> x <- data.table(ID, Count)
>
> # numeric key doesn't work with i argument
> setkey(ID)
[1] 210 210 210 210 210 210 210 210 210 3917 3917 3917 3917 3917 3917
> x[210,list(ID, Count)]
ID Count
1: NA NA
>
> # create character key
> x$charID <- as.character(x$ID)
> setkey(x, charID)
> x["210",list(ID, Count)]
charID ID Count
1: 210 210 1
2: 210 210 1
3: 210 210 0
4: 210 210 1
5: 210 210 1
6: 210 210 1
7: 210 210 1
8: 210 210 1
9: 210 210 1
You need to send the numeric key within a data.table. This is done easily using
J. Or in a listNote that you need to specify the data.table when setting the key
eg
or