0

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

  • 1
    try with `df[[col]] <- coalesce(df[[lcol]],df[[rcol]])` – Edo Jul 31 '20 at 10:43
  • Worked, Thanks! Can you point me towards somewhere that details the significance of the second set of square brackets? – 26slices Jul 31 '20 at 10:51
  • 2
    https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el – Ronak Shah Jul 31 '20 at 10:53

0 Answers0