Search code examples
rdataframerowmultiple-columns

Convert the names rows in the first column to columns in R


I want to move the rows of the first column to the columns and the values of the second column to the rows

###### CREATE THE DATA FRAME ######

Hypothesis <- c("Hyp_1", "Hyp_2", "Hyp_3")
Estimates <- c(0.34, 0.01, 0.22)

df <- as.data.frame(cbind(Hypothesis, Estimates))

df

  Hypothesis Estimates
      Hyp_1      0.34
      Hyp_2      0.01
      Hyp_3      0.22

What I want is the next format

Hyp_1 Hyp_2  Hyp_3
0.34   0.01   0.22

I tried with the next code without success

dcast(df, Estimates~Hypothesis)

Thank you!!!!


Solution

  • Try this:

    library(tidyverse)
    pivot_wider(df, names_from = Hypothesis, values_from = Estimates)