<div dir="ltr">Thank you Mel.<div><br></div><div>str_pad is what i was looking for.</div><div><br></div><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:=stringr::str_pad(value, 6, pad="0", side="right")]</div></div><div><br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 6, 2015 at 10:40 AM, Bacou, Melanie <span dir="ltr"><<a href="mailto:mel@mbacou.com" target="_blank">mel@mbacou.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div 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.<div><div class="h5"><br>
<br>
<div><br>
On 1/5/2015 11:44 PM, Santosh Srinivas wrote:<br>
</div>
</div></div><blockquote type="cite"><div><div class="h5">
<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></fieldset>
<br>
</div></div><pre>_______________________________________________
datatable-help mailing list
<a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">datatable-help@lists.r-forge.r-project.org</a>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a></pre><span class="HOEnZb"><font color="#888888">
</font></span></blockquote><span class="HOEnZb"><font color="#888888">
<br>
<pre cols="72">--
Melanie BACOU
International Food Policy Research Institute
Snr. Program Manager, HarvestChoice
Work <a href="tel:%2B1%28202%29862-5699" value="+12028625699" target="_blank">+1(202)862-5699</a>
E-mail <a href="mailto:m.bacou@cgiar.org" target="_blank">m.bacou@cgiar.org</a>
Visit <a href="http://www.harvestchoice.org" target="_blank">www.harvestchoice.org</a> </pre>
</font></span></div>
</blockquote></div><br></div>