Skip to content
Coming soon: Ally. Your guide to the world of AI and R. Learn More →
R for the Rest of Us Logo

Data Cleaning with R

coalesce() and fill()

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Your Turn

  • Load the fish landings data 'fish-landings.csv'

  • Fill the 'Fish' and 'Lake' columns

  • Reorder the numeric variables ('Commission reported total' first) and create a new column, coalescing the three numeric variables]

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Amal Dahounto

Amal Dahounto • February 28, 2026

Hi Could you kindly give me a case example showing the usefulness of a variable built with coalesce() function?

Gracielle Higino

Gracielle Higino Coach • March 6, 2026

Hi Amal! You can think of coalesce() as a much simpler join or case_when(), if you have multiple columns with the same information that you need to merge to simplify your dataset. That is quite common on survey datasets, where you have columns that are actually different answer options and the cells are populated with the same values as the columns, like in the example below:

| first grade    |   second grade  |
| -------------- | --------------- |
|      NA        |  second grade   |
|      NA        |  second grade   |
|  first grade   |       NA        |

Here are some other use cases:

  • Cleaning survey data: If respondents could answer the same question in multiple fields (e.g., corrected responses), take the first valid response.
  • Combining multiple data sources: If a dataset has the same variable from different systems (e.g., email_primary, email_backup), coalesce() picks the first available value.
  • Filling missing data: Replace NA values in a column using values from another column (e.g., use shipping_address if billing_address is missing).
Amal Dahounto

Amal Dahounto • March 6, 2026

Hi Gracielle Thank you so much for shedding light on this!