0

I have the below tibble.

     A tibble: 2 x 6
  Trial_Type CT_tib_all CT_lum_all CT_tho_all CT_gps_all CT_vest_all
* <fct>           <dbl>      <dbl>      <dbl>      <dbl>       <dbl>
1 Pre             0.244      0.209      0.309      0.315       0.310
2 Post            0.254      0.211      0.302      0.313       0.316

I would like to flip the rows and columns so I end up with a 6 x 2 tibble, but I'm not sure of the easiest way to do this. How do I get the column variable names to become row labels and the row labels as column variables (Pre and Post)?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Ben_89
  • 249
  • 1
  • 8
  • Does this answer your question? [Transposition of a Tibble Using Pivot\_Longer() and Pivot\_Wider (Tidyverse)](https://stackoverflow.com/questions/63345682/transposition-of-a-tibble-using-pivot-longer-and-pivot-wider-tidyverse) – jared_mamrot May 28 '21 at 06:47
  • This is great. Using pivot_longer and pivot_wider in combination does the trick! – Ben_89 May 29 '21 at 01:41

4 Answers4

4

You can use pivot_longer and pivot_wider -

library(dplyr)
library(tidyr)

df %>%
  pivot_longer(cols = -Trial_Type) %>%
  pivot_wider(names_from = Trial_Type, values_from = value)

#  name          Pre  Post
#  <chr>       <dbl> <dbl>
#1 CT_tib_all  0.244 0.254
#2 CT_lum_all  0.209 0.211
#3 CT_tho_all  0.309 0.302
#4 CT_gps_all  0.315 0.313
#5 CT_vest_all 0.31  0.316

In data.table -

library(data.table)

dcast(melt(setDT(df), id.vars  = 'Trial_Type'), 
      variable~Trial_Type, vvalue.var = 'value')
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

t i.e. transpose function in baseR may also be used, in combination with tibble::rownames_to_column and tibble::column_to_rownames

library(tibble)
library(dplyr)


df <- read.table(text = 'Trial_Type CT_tib_all CT_lum_all CT_tho_all CT_gps_all CT_vest_all
        Pre             0.244      0.209      0.309      0.315       0.310
        Post            0.254      0.211      0.302      0.313       0.316', header = T)

df %>% tibble::column_to_rownames('Trial_Type') %>%
  t() %>% as.data.frame() %>%
  rownames_to_column('Trial_Type')
#>    Trial_Type   Pre  Post
#> 1  CT_tib_all 0.244 0.254
#> 2  CT_lum_all 0.209 0.211
#> 3  CT_tho_all 0.309 0.302
#> 4  CT_gps_all 0.315 0.313
#> 5 CT_vest_all 0.310 0.316

Created on 2021-05-28 by the reprex package (v2.0.0)

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
2

We can use transpose from data.table

data.table::transpose(df, make.names = 'Trial_Type', keep.names = 'name')
#         name   Pre  Post
#1  CT_tib_all 0.244 0.254
#2  CT_lum_all 0.209 0.211
#3  CT_tho_all 0.309 0.302
#4  CT_gps_all 0.315 0.313#
5 CT_vest_all 0.310 0.316
akrun
  • 874,273
  • 37
  • 540
  • 662
1

A base R option using reshape

reshape(
  cbind(name = df$Trial_Type, stack(df[-1])),
  direction = "wide",
  idvar = "ind",
  timevar = "name"
)

gives

          ind values.Pre values.Post
1  CT_tib_all      0.244       0.254
3  CT_lum_all      0.209       0.211
5  CT_tho_all      0.309       0.302
7  CT_gps_all      0.315       0.313
9 CT_vest_all      0.310       0.316
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81