[datatable-help] Append missing characters to character variable to bring to a standard length
Santosh Srinivas
santosh.srinivas at gmail.com
Tue Jan 6 05:44:14 CET 2015
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20150106/b2e14970/attachment.html>
More information about the datatable-help
mailing list