将数字转换为特殊格式

zes*_*sla 4 r

在我的数据处理中,我需要执行以下操作:

    #convert '7-25' to '0007 0025'
    #pad 0's to make each four-digit number 
    digits.formatter <- function ('7-25'){.......?}
Run Code Online (Sandbox Code Playgroud)

我不知道如何在R中做到这一点.任何人都可以帮忙吗?

d.b*_*d.b 6

在基础R中,将字符串(或字符串向量)拆分为-,将其部分转换为数字,使用格式化部件sprintf,然后将它们粘贴在一起.

sapply(strsplit(c("7-25", "20-13"), "-"), function(x)
    paste(sprintf("%04d", as.numeric(x)), collapse = " "))
#[1] "0007 0025" "0020 0013"
Run Code Online (Sandbox Code Playgroud)