My question is similar to another one: R replace specific value in many columns across dataframe
However I need to replace values based on a vector from another column rather than with NA or a constant.
Can you please help?
#there are many more yrs in the data set
yr1<-c("1","missing")
yr2<-c("3","4")
right<-c("2","3")
df<-data.frame(yr1,yr2,right)
df<-df %>%
mutate(
across(starts_with('yr'), ~replace(.x, ~.x=="missing", right) ))
Where right
is another column where I want to "lookup" the value to replace the "missing" value.