0

I need to do a pretty common transformation, but I forgot how it is called. Basically I have a matrix:

enter image description here

And I want to get a different matrix with each row to be: gene_id, Vx, num. For instance:

WBGene00000001, V1, 0
WBGene00000002, V2, 0
...

I have a dataframe in R, so how could it be approached there?

Nikita Vlasenko
  • 4,004
  • 7
  • 47
  • 87

1 Answers1

2

This sounds like a pivot/unpivot operation. Easiest implementation is via tidyverse::gather() function:

x = readr::read_csv("gene_id,V1,V2\nWBGene00000001,0,0\nWBGene00000002,0,0")
x

         gene_id    V1    V2
           <chr> <int> <int>
1 WBGene00000001     0     0
2 WBGene00000002     0     0

tidyr::gather(x, varname, value, V1:V2)

         gene_id varname value
           <chr>   <chr> <int>
1 WBGene00000001      V1     0
2 WBGene00000002      V1     0
3 WBGene00000001      V2     0
4 WBGene00000002      V2     0
kgolyaev
  • 565
  • 2
  • 10