Search code examples
rdataframepivottranspose

How to pivot a single column long dataframe into a single row wide dataframe?


I see there are many questions on Stack Overflow and other places online regarding pivoting dataframes into wide and long format. But I don't see any clear, simple examples of just transposing or pivoting a single column dataframe into a single row, "pivot wide" dataframe. Any recommendations for an easy way to do this? I'm fine with base R, dplyr, data.table, tidyr, etc. Below I illustrate what I'm trying to do, with the code for a super-simple single column dataframe at the bottom. This is exactly representative of the larger reactive dataframe I'm wrestling with. Very important to preserve the row.names of my test dataframe and transpose them into column names for the new pivoted data frame, and they need to be column names and not just a first row of a 2 row dataframe.

enter image description here

Code for example dataframe:

test <- data.frame(c(1:5), row.names = LETTERS[1:5])

Solution

  • It is easier after transposing

    as.data.frame(t(unname(test)))
      A B C D E
    1 1 2 3 4 5