0

I use a gene expression package that outputs a named matrix. In order to get this into a tibble, I always have to first cast it to a data.frame so that I can then convert the rownames. Is there a shorter way of doing this? eg:

library(tidyverse)
normalized_counts %>% 
  as.data.frame() %>%
  rownames_to_column('name') %>%
  gather(key = experiment, value = expression, -name) %>%
  as_tibble()

Where I would much prefer to do something like:

library(tidyverse)
normalized_counts %>% 
  as_tibble() %>%
  rownames_to_column('name') %>%
  gather(key = experiment, value = expression, -name)

But I can't because I loose the rownames at the as_tibble step.

kmace
  • 1,994
  • 3
  • 23
  • 39
  • 4
    It is better you provide a small reproducible example. What is `normalized_counts`? Is it a matrix ? If that is the case, `tibble` or `data_frame` strips off the row names and have a default row number as row names where as `as.data.frame` keeps the row names as such. – akrun Dec 29 '17 at 06:24

1 Answers1

1

as_tibble has a rownames arugument you can use.

library(tidyverse)
normalized_counts %>% 
  as_tibble(rownames = 'name') %>%
  gather(key = experiment, value = expression, -name)
kmace
  • 1,994
  • 3
  • 23
  • 39