pad numeric column with leading zeros

后端 未结 2 1092
感情败类
感情败类 2021-01-20 04:41

I\'ve been looking into this for the past few hours. I have tried using sprintf but it changes the column to character. All I want to do is to have a fixed-widt

2条回答
  •  深忆病人
    2021-01-20 05:30

    If you give more context we might be able to help you solve your ultimate (rather than proximal) problem, e.g. if you need output in this format but without quotation marks:

    > cat(sprintf("%04d",5),"\n")
    0005 
    ## or
    > print(sprintf("%04d",5),quote=FALSE)
    [1] 0005
    

    write.csv(...,quote=FALSE) might be helpful too

提交回复
热议问题