-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
  • 81,064
  • 34
  • 182
  • 193
kin182
  • 393
  • 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
  • 91,361
  • 17
  • 137
  • 196
Onyambu
  • 67,392
  • 3
  • 24
  • 53
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
  • 97,041
  • 11
  • 181
  • 245