add leading zeros


I'm looking for a simple way to add leading zeros to integers in a column that range from 1 to n (where n is less than 1000) before then concatenating with a two characters sting (using the join function in the string manipulation node), so that the resulting string is always 6 characters long.

Does anyone know of a simple way of doing this?



This can be done easily with the R integration and the formatC function..


In an R snippet (KNIME2.10+, or with the Labs integration in previous versions), you could add something like:

newcol = formatC($"index",width=6, format="d", flag="0")

knime.out <- cbind(, newcol)

Hi Aaron,

Sounds ideal.

I'm in 2.9 as have yet to test all processes still work in 2.10.

I'll download the nodes & have a look at the R snippet and try your suggested code.




Hello Andy,

For a solution that works for any version of KNIME, you could use a Java Snippet (simple):

return $myString$+String.format("%04d", $myInt$);

Given this table:

myString myInt
df 1
ry 1
gh 10
fg 100
bv 0
hg 789
uy 423
rt ?

here is the output:

myString myInt new column
df 1 df0001
ry 1 ry0001
gh 10 gh0010
fg 100 fg0100
bv 0 bv0000
hg 789 hg0789
uy 423 uy0423
rt ? ?

Note that a value of 0 is ok but that a null will null the output. Also, negative values will have the minus squeezed between your two characters and the padded integer.



Hi Aaron,

I have a problem with the solution, when I try and run the R snippet I get the following errors,


the node hangs and I can't cancel it - and I get a message "Strange state CONFIGURED encountered in cancelExecution()"

the node hangs and I can cancel it - and i get the message "Execute failed: Failed to convert table node inputs into r workspace variables: org.knime.core.node.CanceledExecutionException: ProgressMonitor has been canceled."

or I can't execute at all - and I get the message "Execute failed: voidEval failed, request status: error code: 127"

I have R 3.0.3 installed, and had to install the RServe package and start Rserve using the Knime prompted command; library(Rserve); Rserve(args = "--vanilla").

I'm using the Knime Labs R snippet node.  In the R Snippet node I've kept the code the same excepting two changes, to the variable name I'm using (temp) and the length (4).

  newcol = formatC($"temp",width=4, format="d", flag="0")

  knime.out <- cbind(, newcol)

any help resolving would be much appreciated...



Hi Frederic,

That worked a treat, thank you very much!

I guess I'll leave R integration for another day, though with the way things are, it won't be long!




Thanks for the tip Frederic.  Andy, if you update to 2.10 and still have problems with it, post in the R section and I'll take a look at it in more details.