1

I have the following data frame :

Dependant var independant var p-value
Vd1 Vi1 0.0345
Vd1 Vi2 0.0025
Vd1 Vi3 0.0005
Vd1 Vi4 0.0305
Vd2 Vi1 0.0800
Vd2 Vi2 0.0000
Vd2 Vi3 0.0005
Vd2 Vi4 0.0004

And I would like it to look like that instead :

Dependant var Vi1 Vi2 Vi3 Vi4
Vd1 0.0345 0.0025 0.0005 0.0305
Vd2 0.0800 0.0000 0.0005 0.0004

Is there an easy way to go about it ?

Thanks for your help!

David Potrel
  • 111
  • 8

1 Answers1

3

Does this work:

library("tidyr")
df %>% pivot_wider(Dependant_var, values_from = `p-value`,
                   names_from = independant_var)
# A tibble: 2 × 5
  Dependant_var    Vi1    Vi2    Vi3    Vi4
  <chr>          <dbl>  <dbl>  <dbl>  <dbl>
1 Vd1           0.0345 0.0025 0.0005 0.0305
2 Vd2           0.08   0      0.0005 0.0004
Martin Smith
  • 3,687
  • 1
  • 24
  • 51
Karthik S
  • 11,348
  • 2
  • 11
  • 25