-3

I have a very basic question about reshaping a table:

       pval     Quality
High  0.782        0.62
 Low  0.782        1.58

I would like to change it to

   pval     High     Low
  0.782     0.62    1.58

I am relatively new to R. Could someone help? Thanks!

Jaap
  • 77,147
  • 31
  • 174
  • 185
kin182
  • 371
  • 6
  • 13

2 Answers2

1

You can use the function spread in tidyverse package

 library(tidyverse)
 df1 %>% 
     rownames_to_column() %>% 
     spread(rowname, Quality)

Result

    pval High  Low
 1 0.782 0.62 1.58
David Arenburg
  • 89,637
  • 17
  • 130
  • 188
onyambu
  • 49,350
  • 3
  • 19
  • 45
1

You could do a straight reshape() if you bind the row names to the data first.

reshape(cbind(df, rn=rownames(df), row.names=NULL), 
    direction="wide", timevar="rn", idvar="pval")
#    pval Quality.High Quality.Low
# 1 0.782         0.62        1.58

Data:

df <- structure(list(pval = c(0.782, 0.782), Quality = c(0.62, 1.58
)), .Names = c("pval", "Quality"), class = "data.frame", row.names = c("High", 
"Low"))
Rich Scriven
  • 93,629
  • 10
  • 165
  • 233