The OP requested a 1:1 replacement, i.e., only one data field involved, of selected values. Besides the nested ifelse
approach, this could be done by using factors or join for larger data.
If more than two or three values need to be replaced, the "hard-coded" nested ifelse
approach easily gets unhandy.
Factor case 1: Yes, No
# create some data
loan_status <- c("Fully Paid", "Charged Off", "Something", "Else")
# do the conversion
factor(loan_status, levels = c("Fully Paid", "Charged Off"), labels = c("Yes", "No"))
#[1] Yes No <NA> <NA>
#Levels: Yes No
Or,
as.character(factor(loan_status, levels = c("Fully Paid", "Charged Off"), labels = c("Yes", "No")))
#[1] "Yes" "No" NA NA
if the result is expected as character.
Factor case 2: 0L, 1L as integers
If the result is expected to be of type integer, the factor approach can still be used but needs additonal conversion.
as.integer(as.character(factor(loan_status, levels = c("Fully Paid", "Charged Off"), labels = c("0", "1"))))
#[1] 0 1 NA NA
Note, that the conversion to character is essential here. Otherwise, the result would return the numbers of the factor levels:
as.integer(factor(loan_status, levels = c("Fully Paid", "Charged Off"), labels = c("0", "1")))
#[1] 1 2 NA NA
Join
In case of larger data and many items to be replaced using data.table
join might be an alternative worth considering:
library(data.table)
# create translation table
translation_map <- data.table(
loan_status = c("Fully Paid", "Charged Off"),
target = c(0L, 1L))
# create some user data
DT <- data.table(id = LETTERS[1:4],
loan_status = c("Fully Paid", "Charged Off", "Something", "Else"))
DT
# id loan_status
#1: A Fully Paid
#2: B Charged Off
#3: C Something
#4: D Else
# right join
translation_map[DT, on = "loan_status"]
# loan_status target id
#1: Fully Paid 0 A
#2: Charged Off 1 B
#3: Something NA C
#4: Else NA D
By default (nomatch = NA
), data.table
does a right join, i.e, takes all rows of DT
.