Reputation: 103
I want to replace these 3 characters numbers that begin with 5 to 1. I tried to use a sub if conditional, but it failed
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)
}
If it worked, the output would be something like that:
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
I did that before to remove variables that begin with 4, with the following code:
if((nchar(DO_concatenated$IDADE) == 3)&(funcaoidade(DO_concatenated$IDADE) == 4)){
DO_concatenated$IDADE = sub(pattern = 4, replacement = "", DO_concatenated$IDADE)
}
and it worked!
"funcaoidade" looks for the first character of the number
funcaoidade = function(x){
substr(x, start = 1, stop = 1)
}
so, whats the difference? thanks in advance!
Upvotes: 1
Views: 791
Reputation: 388907
You can use regex to do this :
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
This replaces the 1st digit of 3 digit number which start with 5 to 1. Created a new column AGE1
to compare the output. AGE
column can be overwritten if needed.
data
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))
Upvotes: 1
Reputation: 1261
Here is a way with which you can do it using stringr package;
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
Upvotes: 1