如何在不影响其他数值的情况下,只给特定数值添加一个零?

huangapple go评论48阅读模式
英文:

how can I add a zero only to a specific value without affecting the other values?

问题

如果我只想给特定的值添加一个零,而不影响其他值,前提是我的值有12位数字,我需要它有13位数字,其他值保持10位数字怎么做?

x = c(102030301, 223456783, 5234567123, 916784443214)

if_else(x >= 12, false = paste0("0", x), true = stringr::str_pad(x, 10, side = "left", pad = 0))


我尝试了这段代码,但没有得到预期的结果。

我想要获得以下结果,这是要应用于具有超过1000条记录的数据库的:

"0102030301" "0223456783" "5234567123" "0916784443214"


<details>
<summary>英文:</summary>

how can I add a zero only to a specific value without affecting the other values, this if it fulfills the condition that my value has 12 digits and I need it to have 13 digits and the others 10 digits? 

x=c(102030301,223456783,5234567123,916784443214)

if_else(x>=12, false = paste0("0",x),true = stringr::str_pad(x, 10, side = "left", pad = 0))


I tried this code but it did not give me the expected result.

I would like to obtain the following result this is to apply it to a database with an id of more than 1000 records 

"0102030301" "0223456783" "5234567123" "0916784443214"


</details>


# 答案1
**得分**: 2

1. `x` 是数字,所以当你测试 `x &gt;= 12` 时,它的值是数字,而不是字符的数量。使用 `nchar(x)` 来获取字符的数量。

2. 你的条件是反过来的 - `if_else(x&gt;=12, ..., true = stringr::str_pad(x, 10, side = &quot;left&quot;, pad = 0))` 只会给长度为12的数字填充到长度为10,不会产生任何效果。

3. 我不知道如果输入有11个字符时你想要发生什么...根据你的代码,它将被填充到12个字符。

if_else(nchar(x) >= 12,
true = stringr::str_pad(x, 13, side = "left", pad = 0),
false = stringr::str_pad(x, 10, side = "left", pad = 0)
)

[1] "0102030301" "0223456783" "5234567123" "0916784443214"


在这两种情况下使用 `str_pad` 感觉更安全,因为你说你希望最终的数字位数为13,如果起始为12或更多(尽管这不会减少具有14个或更多位数的输入)。

<details>
<summary>英文:</summary>

You have a couple problems. 

1. `x` is numeric so when you test `x &gt;= 12` its value, not the number of characters. Use `nchar(x)` to get the number of characters.

2. Your conditions are reversed - `if_else(x&gt;=12, ..., true = stringr::str_pad(x, 10, side = &quot;left&quot;, pad = 0))` only padding 12-length numbers up to 10-length... not doing anything.

3. I don&#39;t know what you want to happen if the input has 11 characters... with this code (modeled after yours) it will be padded to 12.

if_else(nchar(x) >= 12,
true = stringr::str_pad(x, 13, side = "left", pad = 0),
false = stringr::str_pad(x, 10, side = "left", pad = 0)
)

[1] "0102030301" "0223456783" "5234567123" "0916784443214"


Using `str_pad` in both cases feels safer, since you say you want the resulting number of digits to be 13 if it starts at 12 or more (though this will not reduce inputs with 14 or more digits).

</details>



# 答案2
**得分**: 1

以下是使用Gregor的方法,但仅限于您的确切示例的简化解决方案的翻译:

```R
x <- c(102030301, 223456783, 5234567123, 916784443214)
x2 <- as.character(x)
k <- sapply(x2, function(z) if (nchar(z) == 12 | nchar(z) == 9) { z = paste0("0", z) } else {z})
k
"0102030301" "0223456783" "5234567123" "0916784443214"

# if you want to get rid of the names
names(k) <- NULL
k
[1] "0102030301" "0223456783" "5234567123" "0916784443214"

请注意,我只翻译了代码中的文本部分。

英文:

Here is a simpler solution using Gregor's approach but limited to your exact examples:

x &lt;- c(102030301,223456783,5234567123,916784443214)
x2 &lt;- as.character(x)
k &lt;- sapply(x2, function(z) if (nchar(z) == 12 | nchar(z) == 9) { z = paste0(&quot;0&quot;, z) } else {z})
k
      102030301       223456783      5234567123    916784443214 
   &quot;0102030301&quot;    &quot;0223456783&quot;    &quot;5234567123&quot; &quot;0916784443214&quot; 

# if you want to get rid of the names
names(k) &lt;- NULL
k
[1] &quot;0102030301&quot;    &quot;0223456783&quot;    &quot;5234567123&quot;    &quot;0916784443214&quot;

huangapple
  • 本文由 发表于 2023年6月13日 00:33:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定