我有一个包含 Reference
列的数据框。这是一个 10 位数字,可以从零开始。
导入 R 时,前导零消失,我想重新添加。
我尝试过使用 sprintf
和 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
最佳答案
library(stringi)
DF = data.frame(Reference = c(102030405,2567894562,235648759), Data = c(10,20,30))
DF$Reference = stri_pad_left(DF$Reference, 10, "0")
DF
# Reference Data
# 1 0102030405 10
# 2 2567894562 20
# 3 0235648759 30
替代解决方案:Adding leading zeros using R。
When importing into R, the leading zeros disappear, which I would like to add back in.
以字符形式读取列可以完全避免这个问题。您可以将 readr::read_csv()
与 col_types
参数一起使用。
关于r - 导入 R 后添加前导零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35844105/