I have a dataframe with a variable containing number of 9 digits. I would like to create a new variable with mutate and ifelse that will depend on the value of the 7th digit. I saw the grepl function that could do what I want but I don't know how to write this code.
For example if the 7th digit is a 1, I would like to have 1 in the new variable, if the 7th digit is a 2, I would like a 5 in the new variable, and if the 7th digit is a 3, I would like a 6 in the new variable.
Please find a example of data below.
Thank you for your help
# Library
library(tidyverse)
# Data
ID=c(1,2,3,4,5,6)
Vectest2=c("9079870989", "907007123", "907865345", "907098432", "907347567", "907845120")
> data
ID Vectest
1 1 830008100
2 2 830056123
3 3 830678309
4 4 830008100
5 5 830056123
6 6 830678309
data=data.frame(ID, Vectest)
# code
data %>% mutate(variable2=ifelse( grepl(), 3,
ifelse( grepl(), 1,
ifelse( grepl(), 2, NA)))
CodePudding user response:
Instead of grepl
I would suggest to you substr
since you have to check fixed position. I have created a new column called seventh_d
which will save the 7th digit in a variable so you don't have to calculate it again and again.
Also, since you are using dplyr
use case_when
which is a better alternative of nested ifelse
.
library(dplyr)
data %>%
mutate(seventh_d = substr(Vectest2, 7, 7),
variable2 = case_when(seventh_d == 1 ~ 1,
seventh_d == 2 ~ 5,
seventh_d == 3 ~ 6,
TRUE ~ NA_real_))
# ID Vectest2 seventh_d variable2
#1 1 9079870989 0 NA
#2 2 907007123 1 1
#3 3 907865345 3 6
#4 4 907098432 4 NA
#5 5 907347567 5 NA
#6 6 907845120 1 1
data
data = data.frame(ID, Vectest2)