我有一个包含一Reference
列的数据框。这是一个 10 位数字,可以从零开始。导入 R 时,前导零消失,我想重新添加。
我尝试过使用sprintf
and formatC
,但每个都有不同的问题。
DF=data.frame(Reference=c(102030405,2567894562,235648759), Data=c(10,20,30))
我得到的输出如下:
> sprintf('%010d', DF$Reference)
[1] "0102030405" " NA" "0235648759"
Warning message:
In sprintf("%010d", DF$Reference) : NAs introduced by coercion
> formatC(DF$Reference, width=10, flag="0")
[1] "001.02e+08" "02.568e+09" "02.356e+08"
当数字已经有 10 位时,第一个输出给出 NA,第二个输出以标准格式存储结果。
我需要的是:
[1] 0102030405 2567894562 0235648759