Row-Binding Data Frames in R With dplyr bind_rows()

Quick Overview

Write three R helpers that stack data frames by rows with dplyr bind_rows, fill missing columns with NA, label each row with its source list name, and return the column names two frames share. It tests how bind_rows aligns columns, what type it returns, and how it treats empty inputs.

Row-Binding Data Frames in R With dplyr bind_rows()

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write three helpers in R that stack data frames by rows using `dplyr::bind_rows()`, filling columns that are missing from an input with `NA`. - `combine_rows(df1, df2)` returns a tibble with the rows of `df2` below the rows of `df1`. The columns are `df1`'s columns in their order, followed by the columns unique to `df2` in their order. Unmatched cells are `NA`. - `combine_labeled(df_list)` binds a named list of data frames by rows and adds a first column `source` (character) holding the list name each row came from. Use the `.id` argument of `bind_rows()`. - `common_columns(df1, df2)` returns the column names present in both frames, sorted ascending, or `character(0)` if there are none. Example: ```r df1 <- data.frame(a = 1, b = 2) df2 <- data.frame(b = 3, c = 4) combine_rows(df1, df2) #> # A tibble: 2 x 3 #> a b c #> <dbl> <dbl> <dbl> #> 1 1 2 NA #> 2 NA 3 4 common_columns(df1, df2) #> [1] "b" ``` ```hint Check the return type Look at what class `bind_rows()` returns when its first input is a plain `data.frame`, and compare that with what `combine_rows` has to return. ``` ### What a Strong Answer Covers - How `bind_rows()` aligns columns by name, fills gaps with `NA`, and why the required column order comes out without manual reordering - Making `combine_rows` return a tibble even when both inputs are plain data frames - Producing the `source` column with `.id` so it is character and sits first - Returning a sorted character vector from `common_columns`, including `character(0)` for no overlap - Edge cases such as zero-row inputs, duplicate list names and an empty list ### Follow-up Questions - How does `bind_rows()` handle type mismatches between columns with the same name? - What happens if the list passed to `combine_labeled` is unnamed, and how would you guard against it? - How would you write `combine_rows` in base R without dplyr?

Overview: Write three R helpers that stack data frames by rows with dplyr bind_rows, fill missing columns with NA, label each row with its source list name, and return the column names two frames share. It tests how bind_rows aligns columns, what type it returns, and how it treats empty inputs.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write three helpers in R that stack data frames by rows using dplyr::bind_rows(), filling columns that are missing from an input with NA.

  • combine_rows(df1, df2) returns a tibble with the rows of df2 below the rows of df1 . The columns are df1 's columns in their order, followed by the columns unique to df2 in their order. Unmatched cells are NA .
  • combine_labeled(df_list) binds a named list of data frames by rows and adds a first column source (character) holding the list name each row came from. Use the .id argument of bind_rows() .
  • common_columns(df1, df2) returns the column names present in both frames, sorted ascending, or character(0) if there are none.

Example:

df1 <- data.frame(a = 1, b = 2)
df2 <- data.frame(b = 3, c = 4)

combine_rows(df1, df2)
#> # A tibble: 2 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     1     2    NA
#> 2    NA     3     4

common_columns(df1, df2)
#> [1] "b"

What a Strong Answer Covers Guidance

  • How bind_rows() aligns columns by name, fills gaps with NA , and why the required column order comes out without manual reordering
  • Making combine_rows return a tibble even when both inputs are plain data frames
  • Producing the source column with .id so it is character and sits first
  • Returning a sorted character vector from common_columns , including character(0) for no overlap
  • Edge cases such as zero-row inputs, duplicate list names and an empty list

Follow-up Questions Guidance

  • How does bind_rows() handle type mismatches between columns with the same name?
  • What happens if the list passed to combine_labeled is unnamed, and how would you guard against it?
  • How would you write combine_rows in base R without dplyr?