How do I transpose a tibble() in R

Alex picture Alex · Mar 14, 2017 · Viewed 21.1k times · Source

In R the t() function is really meant for matrices. When I try to transpose my tibble with t() I end up with a matrix. A matrix can't be made into a tibble with tibble(). I end up spending time storing column names as variables and attaching them as I try to re-make a transposed version of my tibble.

Question: What is the simplest way to transpose a tibble where the first column should become the column names of the new tibble and the old column names become the first column of my new tibble.

Answer

Laurent picture Laurent · Mar 14, 2017

As Sotos has mentioned it, you just need to re-declare your matrix as a tible:

as_tibble(cbind(nms = names(df), t(df)))