I have a dataframe that looks somewhat like the following. A1U_sweet is actually the 19th column in the real dataframe, and C1U_sweet is the 39th column in the real dataframe. T
Consider mapply
to compare A columns and C columns elementwise and assign all B columns at once. And use sub
which unlike gsub
, sub
only replaces first occurrence in case there are A's elsewhere in column header.
new_B_cols <- sub("A", "B", names(df)[grep("^A", names(df))])
replace_na <- function(aa, cc) {
aa[is.na(aa)] <- cc[is.na(aa)]
return(aa)
}
df[new_B_cols] <- mapply(replace_na, df[grep("^A", names(df))], df[grep("^C", names(df))])
df[order(names(df))]
# A1U_sweet A2F_dip A3U_bbq B1U_sweet B2F_dip B3U_bbq C1U_sweet C2F_dip C3U_bbq
# 1 1 2 1 1 2 1 NA NA NA
# 2 NA NA NA 4 1 2 4 1 2
# 3 2 4 7 2 4 7 NA NA NA