将第一个数字字符替换为 sub 和 if 在 R 中



我想替换这 3 个字符以 5 到 1 开头的数字。如果有条件,我尝试使用 sub,但它失败了

DO_concatenated:

DTNASC   AGE
1   3031997  520
2   9022017  0
3   13071933 83
4   6022002  515
5   2061966  50
6   28121946 70
7   4121955  61
8   3101943  73
9   6022017  20
10  14012017 0
11  20071931 8
if((nchar(DO_concatenated$AGE) == 3)&(funcaoidade(DO_concatenated$AGE) == 5)){
DO_concatenated$IDADE = sub(pattern = 5, replacement = 1, DO_concatenated$AGE) 
}

如果它有效,输出将是这样的:

DTNASC   AGE
1   3031997  120
2   9022017  0
3   13071933 83
4   6022002  115
5   2061966  50
6   28121946 70
7   4121955  61
8   3101943  73
9   6022017  20
10  14012017 0
11  20071931 8

我之前这样做是为了删除以 4 开头的变量,使用以下代码:

if((nchar(DO_concatenated$IDADE) == 3)&(funcaoidade(DO_concatenated$IDADE) == 4)){
DO_concatenated$IDADE = sub(pattern = 4, replacement = "", DO_concatenated$IDADE) 
}

它奏效了!

"funcaoidade"寻找数字的第一个字符

funcaoidade = function(x){
substr(x, start = 1, stop = 1)
}

那么,有什么区别呢? 提前感谢!

这是一种使用字符串包的方法;

library(dplyr)
library(stringr)
data <-
data.frame(
DTNASC = c(3031997, 9022017, 13071933, 6022002, 2061966, 28121946, 4121955, 
3101943, 6022017, 14012017, 20071931),
AGE = c(520, 0, 83, 515, 50, 70, 61, 73, 20, 0, 8)
)
data %>%
mutate(# Replacement of Age
# To convert it into character to make it easier
AGE = as.character(AGE),
# Here 5 is the character we are checking in first character
# str_sub(AGE, 1, 1) -> Checks first character
# nchar(AGE) == 3 -> Checks if the length of AGE is 3
# str_replace(AGE, "5", "1") -> Replaces 5 with 1
# as.numeric() -> To convert to a number
AGE = ifelse(str_sub(AGE, 1, 1) == "5" & nchar(AGE) == 3,
as.numeric(str_replace(AGE, "5", "1")),as.numeric(AGE)),
# Replacement of DTNASC
# To convert it into character to make it easier
DTNASC = as.character(DTNASC),
# Here 4 is the character we are checking in first character
# str_sub(DTNASC, 1, 1) -> Checks first character
# nchar(DTNASC) == 7 -> Checks if the length of DTNASC is 7
# str_replace(DTNASC, "4", "") -> Replaces 4 with null
# as.numeric() -> To convert to a number
DTNASC = ifelse(str_sub(DTNASC, 1, 1) == "4" & nchar(DTNASC) == 7,
as.numeric(str_replace(DTNASC, "4", "")),as.numeric(DTNASC)))
# DTNASC AGE
# 3031997 120
# 9022017   0
# 13071933  83
# 6022002 115
# 2061966  50
# 28121946  70
# 121955  61
# 3101943  73
# 6022017  20
# 14012017   0
# 20071931   8

您可以使用正则表达式来执行此操作:

df$AGE1 <- as.integer(sub("^5(..)", "1\1", df$AGE))
df
#     DTNASC AGE AGE1
#1   3031997 520  120
#2   9022017   0    0
#3  13071933  83   83
#4   6022002 515  115
#5   2061966  50   50
#6  28121946  70   70
#7   4121955  61   61
#8   3101943  73   73
#9   6022017  20   20
#10 14012017   0    0
#11 20071931   8    8

这将替换以 5 到 1 开头的 3 位数字的第 1 位数字。创建了一个新列AGE1来比较输出。 如果需要,可以覆盖AGE列。

数据

df <- structure(list(DTNASC = c(3031997, 9022017, 13071933, 6022002, 
2061966, 28121946, 4121955, 3101943, 6022017, 14012017, 20071931
), AGE = c(520, 0, 83, 515, 50, 70, 61, 73, 20, 0, 8)), class = "data.frame", 
row.names = c(NA, -11L))

最新更新