kmace
kmace

Reputation: 2054

Do you need to cast to data.frame in order to convert rownames before casting to tibble

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.

Upvotes: 0

Views: 54

Answers (1)

kmace
kmace

Reputation: 2054

as_tibble has a rownames arugument you can use.

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

Upvotes: 1

Related Questions