I have been trying to write a function in R that can replace strings in particular column by numerals. Following is my example:
d <- data.frame(A = c("D",1,2,3,"D",1,2,"B","D",3,5),
B = c(7,8,9,4,5,8,9,1,6,7,8))
func <- function(dat,rep_val_col,rep_val_col_change,new_val)
{
dat[dat[,rep_val_col] == rep_val_col_change[1],],rep_val_col] = new_val[1]
dat[dat[,rep_val_col] == rep_val_col_change[2],],rep_val_col] = new_val[2]
}
func(d,"A",c("D","B"),new_val = c(9,10))
I want to replace "D" and "B" in column A by 9 and 10 respectively.