在R中,在小数点前添加0作为填充。

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

Add 0 as padding before decimal points in R

问题

我有一个数据框,其中有不同位数的数字。当不同行需要不同数量的前导零以达到小数点前最大3位数字时,我该如何添加前导零。

期望的输出:

  id    col1      col2
1  A       8       008
2  B   125.4     125.4 
3  C    27.1     027.1
4  E    2998      2998
5  F   12341     12341
6  G     7.2     007.2
7  F    6.52    006.52

我已经阅读了许多解决方案,位于 https://stackoverflow.com/questions/63368281/add-leading-zeros-to-numeric-variable-to-the-left-of-the-decimal-point-if-value,但尚未解决我的问题。任何帮助将不胜感激。

英文:

I have a dataframe where the have different total digits. How do I add leading zeroes when different rows need a different number of leads to achieve the max 3 digits if digits beofre decimal point less than 3.

  id    col1 
1  A       8 
2  B   125.4 
3  C    27.1 
4  E    2998
5  F   12341
6  G     7.2
7  F    6.52

Desired Output

  id    col1      col2
1  A       8       008
2  B   125.4     125.4 
3  C    27.1     027.1
4  E    2998      2998
5  F   12341     12341
6  G     7.2     007.2
7  F    6.52    006.52

I've read many solutions in https://stackoverflow.com/questions/63368281/add-leading-zeros-to-numeric-variable-to-the-left-of-the-decimal-point-if-value but have not yet solve my problem.Any help is much appreciated.

答案1

得分: 0

以下是您要翻译的内容:

"Adapting the answer to question linked to here is a way. The main problem I had was with the decimal point when there are none in the input numbers."

"format_special <- function(x, leading = 3) {
fmt <- paste0("%0", leading, "d")
s1 <- sprintf(fmt, as.integer(sub("\..", "", x)))
s2 <- sprintf("%s", sub(".
\.{-1}", "", x))
sprintf("%s%s%s", s1, ifelse(s2 == "", "", "."), s2)
}

format_special(df1$col1)
#> [1] "008" "125.4" "027.1" "2998" "12341" "007.2" "006.52"

df1$col2 <- format_special(df1$col1)
df1
#> id col1 col2
#> 1 A 8.00 008
#> 2 B 125.40 125.4
#> 3 C 27.10 027.1
#> 4 E 2998.00 2998
#> 5 F 12341.00 12341
#> 6 G 7.20 007.2
#> 7 F 6.52 006.52"

"Created on 2023-04-17 with reprex v2.0.2"


"Data"

"df1 <- " id col1
1 A 8
2 B 125.4
3 C 27.1
4 E 2998
5 F 12341
6 G 7.2
7 F 6.52"
df1 <- read.table(text = df1, header = TRUE)"

"Created on 2023-04-17 with reprex v2.0.2"

英文:

Adapting the answer to question linked to here is a way. The main problem I had was with the decimal point when there are none in the input numbers.

format_special &lt;- function(x, leading = 3) {
  fmt &lt;- paste0(&quot;%0&quot;, leading, &quot;d&quot;)
  s1 &lt;- sprintf(fmt, as.integer(sub(&quot;\\..*&quot;, &quot;&quot;, x)))
  s2 &lt;- sprintf(&quot;%s&quot;, sub(&quot;.*\\.{-1}&quot;, &quot;&quot;, x))
  sprintf(&quot;%s%s%s&quot;, s1, ifelse(s2 == &quot;&quot;, &quot;&quot;, &quot;.&quot;), s2)
}

format_special(df1$col1)
#&gt; [1] &quot;008&quot;    &quot;125.4&quot;  &quot;027.1&quot;  &quot;2998&quot;   &quot;12341&quot;  &quot;007.2&quot;  &quot;006.52&quot;

df1$col2 &lt;- format_special(df1$col1)
df1
#&gt;   id     col1   col2
#&gt; 1  A     8.00    008
#&gt; 2  B   125.40  125.4
#&gt; 3  C    27.10  027.1
#&gt; 4  E  2998.00   2998
#&gt; 5  F 12341.00  12341
#&gt; 6  G     7.20  007.2
#&gt; 7  F     6.52 006.52

<sup>Created on 2023-04-17 with reprex v2.0.2</sup>


Data

df1 &lt;- &quot; id    col1 
1  A       8 
2  B   125.4 
3  C    27.1 
4  E    2998
5  F   12341
6  G     7.2
7  F    6.52&quot;
df1 &lt;- read.table(text = df1, header = TRUE)

<sup>Created on 2023-04-17 with reprex v2.0.2</sup>

答案2

得分: 0

ifelse(nchar(sub("\\..*", "", col1)) != 3,
       paste0(str_pad(sub("\\..*", "", col1), 3, "left", "0"), '.', sub(".*\\.", "", col1)),
       col1)
英文:
ifelse(nchar(sub(&quot;\\..*&quot;, &quot;&quot;, col1)) != 3 ,
paste0(str_pad(sub(&quot;\\..*&quot;, &quot;&quot;, col1), 3, &quot;left&quot;, &quot;0&quot;), &#39;.&#39;,sub(&quot;.*\\.&quot;, &quot;&quot;, col1)),col1)```

</details>



huangapple
  • 本文由 发表于 2023年4月17日 13:18:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031917.html
匿名

发表评论

匿名网友

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

确定