<div style="font-size:12.800000190734863px;font-family:arial,sans-serif"><br></div><div style="font-size:12.800000190734863px;font-family:arial,sans-serif">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.</div>
<div style="font-size:12.800000190734863px;font-family:arial,sans-serif"><br></div><div style="font-size:12.800000190734863px;font-family:arial,sans-serif">Thanks to the FAQ I realize that I should be using J to subset and setting the key first, </div>
<div style="font-size:12.800000190734863px;font-family:arial,sans-serif">Thanks to StackOverflow I realize now that I should using J to subset on <a href="http://stackoverflow.com/questions/13666298/why-subsetting-with-a-numeric-key-doesnt-work-with-data-table" style="color:rgb(17,85,204)" target="_blank">numeric keys</a> fixes the problem.</div>
<div style="font-size:12.800000190734863px;font-family:arial,sans-serif"><br></div><div style="font-size:12.800000190734863px;font-family:arial,sans-serif">However, this doesn't explain why using a vector search would sometimes work and sometimes fail.</div>
<div style="font-size:12.800000190734863px;font-family:arial,sans-serif">
<br></div><div style="font-size:12.800000190734863px;font-family:arial,sans-serif"><div><div><font face="arial, helvetica, sans-serif">Thank you,</font></div><div><font face="arial, helvetica, sans-serif"> Gene <font color="#222222">Leynes</font></font></div>
<div><br></div></div><div><br></div><div><div><span style="font-family:'courier new',monospace">> </span></div><div><font face="courier new, monospace">> library(data.table)</font></div><div><font face="courier new, monospace">data.table 1.8.6 For help type: help("data.table")</font></div>
<div><font face="courier new, monospace">> </font></div><div><font face="courier new, monospace">> set.seed(1)</font></div><div><font face="courier new, monospace">> </font></div><div><font face="courier new, monospace">> ## Make an example data table</font></div>
<div><font face="courier new, monospace">> dat = data.table(</font></div><div><font face="courier new, monospace">+ index = 1:1e5, </font></div><div><font face="courier new, monospace">+ groups = sample(letters[1:3], 1e5, replace=TRUE), </font></div>
<div><font face="courier new, monospace">+ values = rnorm(1e5))</font></div><div><font face="courier new, monospace">> </font></div><div><font face="courier new, monospace">> ## Calculate some quantiles for each group</font></div>
<div><font face="courier new, monospace">> dat_quants = dat[</font></div><div><font face="courier new, monospace">+ i=TRUE,</font></div><div><font face="courier new, monospace">+ j=list(</font></div><div>
<font face="courier new, monospace">+ quantile = seq(0,1,.1),</font></div><div><font face="courier new, monospace">+ value = quantile(values, seq(0,1,.1))),</font></div><div><font face="courier new, monospace">+ keyby=groups]</font></div>
<div><font face="courier new, monospace">> </font></div><div><font face="courier new, monospace">> ## Print the 10% 20% and 30% quantiles... but 30% doesn't work</font></div><div><font face="courier new, monospace">> dat_quants[quantile==.1, ]</font></div>
<div><font face="courier new, monospace"> groups quantile value</font></div><div><font face="courier new, monospace">1: a 0.1 -1.284277</font></div><div><font face="courier new, monospace">2: b 0.1 -1.280095</font></div>
<div><font face="courier new, monospace">3: c 0.1 -1.291173</font></div><div><font face="courier new, monospace">> dat_quants[quantile==.2, ]</font></div><div><font face="courier new, monospace"> groups quantile value</font></div>
<div><font face="courier new, monospace">1: a 0.2 -0.8413631</font></div><div><font face="courier new, monospace">2: b 0.2 -0.8397591</font></div><div><font face="courier new, monospace">3: c 0.2 -0.8423560</font></div>
<div><font face="courier new, monospace" color="#ff0000">> dat_quants[quantile==.3, ]</font></div><div><font face="courier new, monospace" color="#ff0000">Empty data.table (0 rows) of 3 cols: groups,quantile,value</font></div>
<div><font face="courier new, monospace">> </font></div><div><font face="courier new, monospace">> </font></div><div><font face="courier new, monospace">> ## Changing to character will allow all of them to work</font></div>
<div><font face="courier new, monospace">> dat_quants$quantile = as.character(dat_quants$quantile)</font></div><div><font face="courier new, monospace">> </font></div><div><font face="courier new, monospace"><br></font></div>
<div><font face="courier new, monospace">> sessionInfo()</font></div><div><font face="courier new, monospace">R version 2.15.2 (2012-10-26)</font></div><div><font face="courier new, monospace">Platform: x86_64-w64-mingw32/x64 (64-bit)</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">locale:</font></div><div><font face="courier new, monospace">[1] LC_COLLATE=English_United States.1252 </font></div><div>
<font face="courier new, monospace">[2] LC_CTYPE=English_United States.1252 </font></div>
<div><font face="courier new, monospace">[3] LC_MONETARY=English_United States.1252</font></div><div><font face="courier new, monospace">[4] LC_NUMERIC=C </font></div><div><font face="courier new, monospace">[5] LC_TIME=English_United States.1252 </font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">attached base packages:</font></div><div><font face="courier new, monospace">[1] stats graphics grDevices utils datasets methods base </font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">other attached packages:</font></div><div><font face="courier new, monospace">[1] data.table_1.8.6 geneorama_1.0 </font></div>
<div><font face="courier new, monospace">> </font></div><div><font face="courier new, monospace">> </font></div></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace"><br></font></div></div>