简体   繁体   English

仅当 R dataframe 中的数字以 6 开头时才添加前导零

[英]Add leading zeros only if the number starts with 6 in R dataframe

I have a dataframe with numbers that I need to format.我有一个 dataframe 需要格式化的数字。 How do I add leading zeroes to only numbers that starts with 6?如何将前导零添加到仅以 6 开头的数字? All examples seen using str_pad() or sprintf() are not exactly like my task, and I found it challenging to adapt them.使用str_pad()sprintf()看到的所有示例都与我的任务不完全一样,我发现调整它们具有挑战性。 My dummy dataframe is below:我的虚拟 dataframe 如下:

dummy_numbers
621103 
06102658  
19562106    
61102
0635467

The desired result is:期望的结果是:

desired_numbers
0621103 
06102658  
19562106    
061102
0635467

Thanks.谢谢。

You may add 0 to only those numbers that start with 6. This can be written as -您只能将 0 添加到以 6 开头的数字。这可以写成 -

transform(df, dummy_numbers = 
              paste0(ifelse(grepl('^6', dummy_numbers), "0", ""), dummy_numbers))

#  dummy_numbers
#1       0621103
#2      06102658
#3      19562106
#4        061102
#5       0635467

Without ifelse -没有ifelse -

inds <- grepl('^6', df$dummy_numbers)
df$dummy_numbers[inds] <- paste0(0, df$dummy_numbers[inds])
df

Your can use grepl() and regex ( ^ ) to capture the start of a string.您可以使用grepl()和正则表达式 ( ^ ) 来捕获字符串的开头。

library(tidyverse)

df %>% mutate(dummy_numbers = ifelse(grepl("^6", dummy_numbers), 
                                     paste0(0, dummy_numbers), 
                                     dummy_numbers))

We just need a simple "leading-6" regex:我们只需要一个简单的“leading-6”正则表达式:

gsub("^6", "06", dummy)
# [1] "0621103"  "06102658" "19562106" "061102"   "0635467" 

identical(gsub("^6", "06", dummy), desired)
# [1] TRUE

Data数据

dummy <- c("621103", "06102658", "19562106", "61102", "0635467")
desired <- c("0621103", "06102658", "19562106", "061102", "0635467")

Another option is to use str_replace to capture numbers starting with 6 then replace with 06 :另一种选择是使用str_replace捕获以6开头的数字,然后替换为06

library(tidyverse)

df %>% 
  mutate(dummy_numbers = str_replace(dummy_numbers, "^6", "06"))

Output Output

  dummy_numbers
1       0621103
2      06102658
3      19562106
4        061102
5       0635467

Data数据

df <-
  structure(list(dummy_numbers = c(
    "621103", "06102658", "19562106", 
    "61102", "0635467"
  )),
  class = "data.frame",
  row.names = c(NA,-5L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM