英文:
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 <- 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
<sup>Created on 2023-04-17 with reprex v2.0.2</sup>
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)
<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("\\..*", "", col1)) != 3 ,
paste0(str_pad(sub("\\..*", "", col1), 3, "left", "0"), '.',sub(".*\\.", "", col1)),col1)```
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论