[datatable-help] Append missing characters to character variable to bring to a standard length

Santosh Srinivas santosh.srinivas at gmail.com
Tue Jan 6 09:36:52 CET 2015


Thank you Mel.

str_pad is what i was looking for.

require("data.table")
value <- sample(seq(60:1000),100) #Random data
dt <- as.data.table(value)
dt[,value:=stringr::str_pad(value, 6, pad="0", side="right")]



On Tue, Jan 6, 2015 at 10:40 AM, Bacou, Melanie <mel at mbacou.com> wrote:

>  substr() needs argument fixed=TRUE or else it uses regex expression for
> string matching and replacements.
> 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.
> --Mel.
>
>
>
> On 1/5/2015 11:44 PM, Santosh Srinivas wrote:
>
> Hello All,
>
>  I am trying to create a character variable in my data table so that it
> is *atleast* 6 characters long.
> The last line seems to be going wrong.
>
>  Please advise. Reproducible code below
>
>  require("data.table")
> value <- sample(seq(60:1000),100) #Random data
> dt <- as.data.table(value)
> dt[,value:=as.character(value)] #Cast as character
> dt[, value_MISSINGDIGITS:=6-nchar(value)] #Check for # of characters
> missing
> dt[, value_MISSINGDIGITS:=value_MISSINGDIGITS*(value_MISSINGDIGITS>0)]
> #Handle negative values
>
>  # This works till here!
> # The missing character count works correctly above
>
>  # The below fails. I am trying to generate dummy 0s to fill the missing
> characters. The values do not get generated accurately
> dt[,value_MISSINGPART:=substr("000000",0,value_MISSINGDIGITS)]
>
>
>  > sessionInfo()
> R version 3.1.2 (2014-10-31)
> Platform: x86_64-apple-darwin13.4.0 (64-bit)
>
>  locale:
> [1] C
>
>  attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
>  other attached packages:
>  [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
>  [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
>
>  loaded via a namespace (and not attached):
>  [1] Rcpp_0.11.3          chron_2.3-45         digest_0.6.4
> futile.options_1.0.0 grid_3.1.2
>  [6] lattice_0.20-29      memoise_0.2.1        plyr_1.8.1
> reshape2_1.4         stringr_0.6.2
> [11] tools_3.1.2
>
>
> _______________________________________________
> datatable-help mailing listdatatable-help at lists.r-forge.r-project.orghttps://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>
>
> --
> Melanie BACOU
> International Food Policy Research Institute
> Snr. Program Manager, HarvestChoice
> Work +1(202)862-5699
> E-mail m.bacou at cgiar.org
> Visit www.harvestchoice.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20150106/823f5863/attachment-0001.html>


More information about the datatable-help mailing list