I am trying to recode a list of columns var1:var8 in df - "sampledf" where I am changing the values "B" and "D" into "0", but keeping the other values as it is.
sampledf <- data.frame(
var1 = c(1,4,2,1,1,0,0,1,0,0,0),
var2 = c(1,1,"D",1,0,0,1,"B",0,"D",0),
var3 = c(1,5,2,1,"B",0,1,1,1,0,0),
var4 = c(1,1,0,1,2,0,1,1,5,1,1),
var5 = c(0,4,"D",1,0,0,0,1,1,1,1),
var6 = c(1,"D",0,1,0,2,1,1,0,1,0),
var7 = c(1,1,0,0,1,"E",1,0,"D",1,1),
var8 = c(1,1,0,0,2,5,1,"D",0,3,1))
This is what I tried but did not work. Compared to this example, the other values I have in my real dataset is very very long. So I cannot manually supply all the values. All I want is just to change this and keep others as it is.
sampledfnew <- sampledf %>% mutate(across(var1:var2, ~recode(
.x,
'B'=0L,
'D'=0L,
TRUE ~ X,
)))
Can anyone help me fix the error here? Thank you