<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    substr() needs argument fixed=TRUE or else it uses regex expression
    for string matching and replacements.<br>
    I'd suggest you look into stringr::str_pad(x, 6) or formatC(x, width
    = 6, format = "d", flag = "0") for faster ways to achieve this.<br>
    --Mel.<br>
    <br>
    <div class="moz-cite-prefix"><br>
      On 1/5/2015 11:44 PM, Santosh Srinivas wrote:<br>
    </div>
    <blockquote
cite="mid:CALtLFXTtJfbDXXwwNn5xSQX+A9UjofwremAVdYVe1jyf=fSRWg@mail.gmail.com"
      type="cite">
      <div dir="ltr">Hello All,
        <div><br>
        </div>
        <div>I am trying to create a character variable in my data table
          so that it is *atleast* 6 characters long.</div>
        <div>The last line seems to be going wrong.</div>
        <div><br>
        </div>
        <div>Please advise. Reproducible code below</div>
        <div><br>
        </div>
        <div>
          <div>require("data.table")</div>
          <div>value <- sample(seq(60:1000),100) #Random data</div>
          <div>dt <- as.data.table(value)</div>
          <div>dt[,value:=as.character(value)] #Cast as character</div>
          <div>dt[, value_MISSINGDIGITS:=6-nchar(value)] #Check for # of
            characters missing</div>
          <div>dt[,
            value_MISSINGDIGITS:=value_MISSINGDIGITS*(value_MISSINGDIGITS>0)]
            #Handle negative values</div>
          <div><br>
          </div>
          <div># This works till here!</div>
          <div># The missing character count works correctly above</div>
          <div><br>
          </div>
          <div># The below fails. I am trying to generate dummy 0s to
            fill the missing characters. The values do not get generated
            accurately</div>
          <div>dt[,value_MISSINGPART:=substr("000000",0,value_MISSINGDIGITS)]</div>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div>
          <div>> sessionInfo()</div>
          <div>R version 3.1.2 (2014-10-31)</div>
          <div>Platform: x86_64-apple-darwin13.4.0 (64-bit)</div>
          <div><br>
          </div>
          <div>locale:</div>
          <div>[1] C</div>
          <div><br>
          </div>
          <div>attached base packages:</div>
          <div>[1] stats     graphics  grDevices utils     datasets
             methods   base     </div>
          <div><br>
          </div>
          <div>other attached packages:</div>
          <div> [1] fractalrock_1.1.0   quantmod_0.4-0      TTR_0.22-0  
                   xts_0.9-7           zoo_1.7-11        
             Defaults_1.1-1     </div>
          <div> [7] futile.logger_1.3.7 futile.any_1.3.0  
             lambda.r_1.1.6      timeDate_3011.99    lubridate_1.3.3    
            data.table_1.9.4   </div>
          <div><br>
          </div>
          <div>loaded via a namespace (and not attached):</div>
          <div> [1] Rcpp_0.11.3          chron_2.3-45        
            digest_0.6.4         futile.options_1.0.0 grid_3.1.2        
             </div>
          <div> [6] lattice_0.20-29      memoise_0.2.1        plyr_1.8.1
                      reshape2_1.4         stringr_0.6.2       </div>
          <div>[11] tools_3.1.2    </div>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
datatable-help mailing list
<a class="moz-txt-link-abbreviated" href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a>
<a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a></pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Melanie BACOU
International Food Policy Research Institute
Snr. Program Manager, HarvestChoice
Work +1(202)862-5699
E-mail <a class="moz-txt-link-abbreviated" href="mailto:m.bacou@cgiar.org">m.bacou@cgiar.org</a>
Visit <a class="moz-txt-link-abbreviated" href="http://www.harvestchoice.org">www.harvestchoice.org</a> </pre>
  </body>
</html>