Combine Data Frames by Columns and Rows in R

Quick Overview

Write three base R helpers that bind data frames side by side, stack two frames whose columns come in a different order, and row-bind a whole list of frames. It tests input guards with stop(), matching columns by name, and resetting row names after rbind.

Combine Data Frames by Columns and Rows in R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement three R functions that validate and combine data frames column-wise or row-wise. - `combine_columns(df1, df2)` returns `cbind` of `df1` then `df2`. If the row counts differ, it calls `stop()`. - `combine_rows(df1, df2)` reorders the columns of `df2` to match the order in `df1`, then calls `rbind`. If the two sets of column names differ (order ignored), it calls `stop()`. - `stack_many(df_list)` row-stacks all frames in list order with `do.call(rbind, ...)`. An empty list triggers `stop()`. - Row-wise results must have sequential row names 1 to N, not the row names carried over from the inputs. Example: ```r a <- data.frame(x = 1:2, y = c("a", "b")) b <- data.frame(z = c(TRUE, FALSE)) combine_columns(a, b) # x y z # 1 1 a TRUE # 2 2 b FALSE c <- data.frame(y = "c", x = 3) # different column order combine_rows(a, c) # x y # 1 1 a # 2 2 b # 3 3 c ``` ```hint Silent failures Check what `cbind` does with a 1-row frame next to a 2-row frame, and what row names `rbind` produces when its inputs carry their own row names. ``` ### What a Strong Answer Covers - Guards that fail with `stop()` before `cbind` or `rbind` can recycle rows or raise a confusing error. - Comparing the column-name sets with order ignored, then aligning `df2` to the column order of `df1` by name. - Resetting row names so every row-wise result is numbered 1 to N. - Handling an empty list and keeping list order in `stack_many`. ### Follow-up Questions - How would you row-bind two frames whose column sets differ, filling the missing columns with `NA`? - What happens to a column that is integer in one frame and character in the other, and how would you catch it? - Why is growing a data frame with `rbind` inside a loop slow, and what would you do instead?

Overview: Write three base R helpers that bind data frames side by side, stack two frames whose columns come in a different order, and row-bind a whole list of frames. It tests input guards with stop(), matching columns by name, and resetting row names after rbind.

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

Implement three R functions that validate and combine data frames column-wise or row-wise.

  • combine_columns(df1, df2) returns cbind of df1 then df2 . If the row counts differ, it calls stop() .
  • combine_rows(df1, df2) reorders the columns of df2 to match the order in df1 , then calls rbind . If the two sets of column names differ (order ignored), it calls stop() .
  • stack_many(df_list) row-stacks all frames in list order with do.call(rbind, ...) . An empty list triggers stop() .
  • Row-wise results must have sequential row names 1 to N, not the row names carried over from the inputs.

Example:

a <- data.frame(x = 1:2, y = c("a", "b"))
b <- data.frame(z = c(TRUE, FALSE))
combine_columns(a, b)
#   x y     z
# 1 1 a  TRUE
# 2 2 b FALSE

c <- data.frame(y = "c", x = 3)   # different column order
combine_rows(a, c)
#   x y
# 1 1 a
# 2 2 b
# 3 3 c

What a Strong Answer Covers Guidance

  • Guards that fail with stop() before cbind or rbind can recycle rows or raise a confusing error.
  • Comparing the column-name sets with order ignored, then aligning df2 to the column order of df1 by name.
  • Resetting row names so every row-wise result is numbered 1 to N.
  • Handling an empty list and keeping list order in stack_many .

Follow-up Questions Guidance

  • How would you row-bind two frames whose column sets differ, filling the missing columns with NA ?
  • What happens to a column that is integer in one frame and character in the other, and how would you catch it?
  • Why is growing a data frame with rbind inside a loop slow, and what would you do instead?