<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>