Trying to use a for loop to coalesce columns after full join:
df <- full_join(df1, df2, by = "user_name")
Columns in df1 and df2 that have the same name are then split out as colname.x and colname.y and I'd like to create a column which is a coalesce of the two. In particular I'd like to use a for loop to do this. Something like:
coalesce_cols <- c("country", "currency","segment","user_id","first_name","last_name","team_name")
for (col in coalesce_cols){
lcol <- paste0(col, ".x")
rcol <- paste0(col, ".y")
df[col] <- coalesce(df[lcol],df[rcol])
}
but am getting the following error:
Error: Must subset elements with a valid subscript vector.
x Subscript must be a simple vector, not a matrix.
Not sure how to proceed. Is there a simple way to coalesce the columns after a full join? Thanks in advance