I have the following data frame (example):
myfile <- data.frame(C1=c(1,3,4,5),
C2=c(5,4,6,7),
C3=c(0,1,3,2),
C1_A=c(NA,NA,1,2),
C2_A=c(NA,9,8,7),
C3_A=c(NA,NA,NA,1))
I would like to replace all NA values under the last 3 "_A" columns with the respective same row value from columns C1 to C3. for example C1_A to be 1,3,1,2
I tried the following line
myfile <- myfile %>% mutate(across(c(C1_A:C3_A), ~ if_else(is.na(.)==TRUE, eval(parse(text=str_replace(., "_A", ""))), .)))
but is not working and returns the bottom row value of the _A columns. Also tried it with the rowwise dplyr option, but still no success.
My real dataset has several columns like the example, so doesn't make sense to mutate each individually. How best to resolve this?