1

I'm a trying to manage a big data.frame to reshape it. I tried some little things, but as I'm newbie in R, I cannot do it as I wish

DF1 <- data.frame(V1 = c("Sample1", "Sample1", "Sample2", "Sample2"),
                  V2 = c("Gene1", "Gene2", "Gene1", "Gene2"), 
                  V3 = c(12, 12, 12, 12))

I would like to reshape it as :

DF2 <- data.frame(V1 = c("NA", "Gene1", "Gene2"), 
                  V2 = c("Sample1", 12, 12), 
                  V3 = c("Sample2", 12, 12))

I tried to dissociate the table in small tab containing all the gene information for 1 sample and after merge them to place the gene in row. But is there a simplest way to do that?

Thanks a lot,

Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
Nicolas
  • 23
  • 6
  • Would something like: `dcast(DF1, Gene~Sample)` work for you? – Mike H. Dec 13 '17 at 14:32
  • Oh yes :D in this example it works with dcast(DF1, V2~V1). Thanks a lot!!!!! – Nicolas Dec 13 '17 at 14:37
  • The resulting dataframe is not exactly what you want, but you probably want to have `Sample1` and `Sample2` as column names not as data entries. In the `tidyverse` spirit, you can solve your problem as following: `library(tidyverse); spread(DF1, key = V1, value = V3)` Welcome to R! – D Pinto Dec 13 '17 at 14:44
  • yap, it could be a solution too. I need to read the vignette of the package to manage some identical values, because it seem to doesn't support them :) thanks – Nicolas Dec 13 '17 at 14:53

0 Answers0