[datatable-help] Subsetting bug?
Gene Leynes
gleynes+r at gmail.com
Fri Jan 11 16:10:40 CET 2013
Yesterday I was having a problem subsetting based on a numeric key. I had
some quantile data and I could get the 10% and 20%, but getting the 30%
failed. I was using quantile==.1, quantile==.2, etc.
Thanks to the FAQ I realize that I should be using J to subset and setting
the key first,
Thanks to StackOverflow I realize now that I should using J to subset
on numeric
keys<http://stackoverflow.com/questions/13666298/why-subsetting-with-a-numeric-key-doesnt-work-with-data-table>
fixes
the problem.
However, this doesn't explain why using a vector search would sometimes
work and sometimes fail.
Thank you,
Gene Leynes
>
> library(data.table)
data.table 1.8.6 For help type: help("data.table")
>
> set.seed(1)
>
> ## Make an example data table
> dat = data.table(
+ index = 1:1e5,
+ groups = sample(letters[1:3], 1e5, replace=TRUE),
+ values = rnorm(1e5))
>
> ## Calculate some quantiles for each group
> dat_quants = dat[
+ i=TRUE,
+ j=list(
+ quantile = seq(0,1,.1),
+ value = quantile(values, seq(0,1,.1))),
+ keyby=groups]
>
> ## Print the 10% 20% and 30% quantiles... but 30% doesn't work
> dat_quants[quantile==.1, ]
groups quantile value
1: a 0.1 -1.284277
2: b 0.1 -1.280095
3: c 0.1 -1.291173
> dat_quants[quantile==.2, ]
groups quantile value
1: a 0.2 -0.8413631
2: b 0.2 -0.8397591
3: c 0.2 -0.8423560
> dat_quants[quantile==.3, ]
Empty data.table (0 rows) of 3 cols: groups,quantile,value
>
>
> ## Changing to character will allow all of them to work
> dat_quants$quantile = as.character(dat_quants$quantile)
>
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.8.6 geneorama_1.0
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20130111/bde16030/attachment.html>
More information about the datatable-help
mailing list