0

I have a dataframe:

                 [,1]
A01              127281
A02              328077
A03              99573
A04               88251
concatanated_data 34318

I am getting the dimensions as:

dim(mapped_reads)
[1] 5 1

and rownames as

rownames(mapped_reads)
[1] "1"

I want the first column to say "Samples" and the second column to say "Counts". How do I rename the rows when it is reading the dataframe as having only one column?

Indu
  • 1
  • 1
    That looks like a matrix, not a data frame. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with more easily. Workable versions of your data (not printouts of it) are helpful especially with data type issues. – camille Apr 18 '19 at 13:17

1 Answers1

0

As mentioned by camille in the comments - it looks like your data set is a matrix, so we'll start there. We'll use a few packages from the tidyverse to accomplish your desired output:

library(tidyverse)

mapped_reads <- matrix(c(127281L, 328077L, 99573L, 88251L, 34318L))
rownames(mapped_reads) <- c("A01", "A02", "A03", "A04", "concatanated_data")

mapped_reads
#>                     [,1]
#> A01               127281
#> A02               328077
#> A03                99573
#> A04                88251
#> concatanated_data  34318

df <- mapped_reads %>%
  as_tibble(rownames = "Sample") %>%
  rename("Count" = 2)

df
#> # A tibble: 5 x 2
#>   Sample             Count
#>   <chr>              <int>
#> 1 A01               127281
#> 2 A02               328077
#> 3 A03                99573
#> 4 A04                88251
#> 5 concatanated_data  34318

Created on 2019-04-18 by the reprex package (v0.2.1)

tomasu
  • 1,388
  • 9
  • 11