I built an R function that produces the wrong output. Here is a simplified version:
# Dataset
test <- data.frame( color = c("blue", "green", "black", "white", "gray", "purple"))
test$color <- as.character(test$color)
# Build function to recode values which matches the input into a new output
fun_recode <- function(regex_input, regex_output) {
reg_start <- ".*"
reg_end <- ".*"
index <- vector()
for (i in 1:length(regex_input)) {
index_1 <- grep(pattern = paste0(reg_start, regex_input[i], reg_end), x = test$color)
index <- c(index, index_1)
}
test[index, ] <- regex_output
}
# Set arguments
regex_input <- c("a", "y")
regex_output <- "GOT IT!"
# Call function
fun_recode(regex_input, regex_output)
I get this output (noting changed):
The correct output should be:
Please advise what is wrong with my function. Thanks in advance.

