从" 1"更改列变量字符串到" 01"

时间:2017-06-11 20:11:22

标签: r dataframe

我在数据框中有一个charachter类型的列。变量是1,01,2,01,3,3,0,4,0等的混合物。

如果没有领先的' 0'和格式所以它们都是01,02,03,04等

3 个答案:

答案 0 :(得分:4)

我们需要先将其转换为numeric并使用sprintf

df1$col1 <- sprintf("%02d", as.numeric(df1$col1))
df1$col1
#[1] "01" "01" "02" "01" "03" "03" "04" "04"

如果是factor列,请先转换为character,然后转到numeric

df1$col1 <- sprintf("%02d", as.numeric(as.character(df1$col1)))

如果包含信件

df1$col1 <- c(1, '01', 2, '01', 3, 'A', 4, '04')
i1 <- grepl("^[0-9]$", df1$col1)
df1$col1[i1] <- paste0("0", df1$col1[i1])
df1$col1
#[1] "01" "01" "02" "01" "03" "A"  "04" "04"

数据

df1 <- data.frame(col1 = c(1, '01', 2, '01', 3, '03', 4, '04'), stringsAsFactors=FALSE)

答案 1 :(得分:3)

 vec<-c("01","1","2","03","05","3","4","A","B","XX")
 >vec

[1]“01”“1”“2”“03”“05”“3”“4”“A”“B”“XX”

然后:

 ifelse(nchar(vec)!=2,paste0("0",vec),vec)

[1]“01”“01”“02”“03”“05”“03”“04”“0A”“0B”“XX”

编辑(仅检查数字。保持字符不变)

 ifelse((nchar(vec)!=2 &!is.na(as.numeric(vec))) ,paste0("0",vec),vec)

[1]“01”“01”“02”“03”“05”“03”“04”“A”“B”“XX”

答案 2 :(得分:3)

col1是数字时,此正则表达式解决方案插入0:

df1 <- data.frame(
      col1 = c(1, '01', 2, '01', 3, '03', 4, '04','A','XX'),
         stringsAsFactors = FALSE)

df1$col1 <- gsub("(\\d)+", "0\\1", df1$col1)
df1$col1
# [1] "01" "01" "02" "01" "03" "03" "04" "04" "A"  "XX"