Transpose a matrix, check symmetry and pivot a labelled data frame in base R

Quick Overview

Write three base R helpers: a matrix transpose, a symmetry check that ignores dimnames, and a function that transposes a labelled data frame into one column per id. It tests matrix attributes, exact value comparison, and how R handles column names and types when reshaping.

Transpose a matrix, check symmetry and pivot a labelled data frame in base R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

In `/app/solution.R`, implement three transpose helpers using only base R: a matrix transpose, a symmetry check, and a labelled data-frame pivot. Requirements: - `transpose_matrix(m)` returns the transpose of the numeric matrix `m` (equivalent to `t()`). - `is_symmetric(m)` takes a square numeric matrix and returns a single logical: `TRUE` if the matrix equals its transpose by value (ignoring attributes and dimnames), otherwise `FALSE`. - `transpose_named_df(df, id_col)` returns a transposed data.frame, where `id_col` is a length-1 character string. - Its first column, `"variable"`, is character (not factor) and holds the names of the non-id columns. - It then has one column per value of `df[[id_col]]`, named by that value and in the original row order, holding the transposed values. Example: ```r m <- matrix(1:6, nrow = 2) # 2x3 transpose_matrix(m) # 3x2 transpose # [,1] [,2] # [1,] 1 2 # [2,] 3 4 # [3,] 5 6 is_symmetric(matrix(c(1,2,2,1), 2)) # TRUE is_symmetric(matrix(1:4, 2)) # FALSE df <- data.frame(id = c("a","b"), x = c(1, 3), y = c(2, 4), stringsAsFactors = FALSE) transpose_named_df(df, "id") # variable a b # 1 x 1 3 # 2 y 2 4 ``` ```hint Watch the attributes and the names For the symmetry check, look at what `t()` does to dimnames and make sure they cannot change the answer. For the data frame, check what `data.frame()` does to a column name such as `"2024-01"` before you rely on it to name the id columns. ``` ### Constraints & Assumptions - Base R only, with no packages. - `m` is a numeric matrix, and `is_symmetric()` receives a square one. - `id_col` is a length-1 character string that names a column of `df`, and the values in that column are unique. ### What a Strong Answer Covers - A transpose that swaps rows and columns, including their dimnames. - A symmetry check that compares values only, returns exactly one `TRUE` or `FALSE`, and states how it treats non-square input and `NA`. - A data frame whose first column is the character `variable` column, followed by one column per id, named verbatim and in original row order. - What happens to the values when the non-id columns do not share a type. ### Follow-up Questions - How would you handle a `df` whose non-id columns have mixed types? - When would you compare with a tolerance in `is_symmetric()`, and how would you do it? - What goes wrong if an id value is `"variable"` or the ids are not unique, and how would you guard against it?

Overview: Write three base R helpers: a matrix transpose, a symmetry check that ignores dimnames, and a function that transposes a labelled data frame into one column per id. It tests matrix attributes, exact value comparison, and how R handles column names and types when reshaping.

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

In /app/solution.R, implement three transpose helpers using only base R: a matrix transpose, a symmetry check, and a labelled data-frame pivot.

Requirements:

  • transpose_matrix(m) returns the transpose of the numeric matrix m (equivalent to t() ).
  • is_symmetric(m) takes a square numeric matrix and returns a single logical: TRUE if the matrix equals its transpose by value (ignoring attributes and dimnames), otherwise FALSE .
  • transpose_named_df(df, id_col) returns a transposed data.frame, where id_col is a length-1 character string.
  • Its first column, "variable" , is character (not factor) and holds the names of the non-id columns.
  • It then has one column per value of df[[id_col]] , named by that value and in the original row order, holding the transposed values.

Example:

m <- matrix(1:6, nrow = 2)      # 2x3
transpose_matrix(m)             # 3x2 transpose
#      [,1] [,2]
# [1,]    1    2
# [2,]    3    4
# [3,]    5    6

is_symmetric(matrix(c(1,2,2,1), 2))  # TRUE
is_symmetric(matrix(1:4, 2))         # FALSE

df <- data.frame(id = c("a","b"), x = c(1, 3), y = c(2, 4),
                 stringsAsFactors = FALSE)
transpose_named_df(df, "id")
#   variable a b
# 1        x 1 3
# 2        y 2 4

Constraints & Assumptions

  • Base R only, with no packages.
  • m is a numeric matrix, and is_symmetric() receives a square one.
  • id_col is a length-1 character string that names a column of df , and the values in that column are unique.

What a Strong Answer Covers Guidance

  • A transpose that swaps rows and columns, including their dimnames.
  • A symmetry check that compares values only, returns exactly one TRUE or FALSE , and states how it treats non-square input and NA .
  • A data frame whose first column is the character variable column, followed by one column per id, named verbatim and in original row order.
  • What happens to the values when the non-id columns do not share a type.

Follow-up Questions Guidance

  • How would you handle a df whose non-id columns have mixed types?
  • When would you compare with a tolerance in is_symmetric() , and how would you do it?
  • What goes wrong if an id value is "variable" or the ids are not unique, and how would you guard against it?