Home > Enterprise >  Get a row in R data frame by name
Get a row in R data frame by name

Time:04-17

I'm just beginning to learn R and have some trouble retrieving a row in data frame by name. Below is the console output. Why doesn't df[c("0610007C21Rik"),] give the same output as df[1,] and what should I do retrieve the row by name? Thanks ahead.

> df
# A tibble: 7,236 × 2
   gene          means
   <chr>         <dbl>
 1 0610007C21Rik  22.2
 2 0610007L01Rik  72.1
 3 0610007P08Rik  75.2
 4 0610007P14Rik  20.3
 5 0610009B22Rik  44.4
 6 0610009D07Rik  42.8
 7 0610009O20Rik  53.7
 8 0610010O12Rik  57.6
 9 0610011F06Rik  29.4
10 0610030E20Rik  66.0
# … with 7,226 more rows
> df[1,]
# A tibble: 1 × 2
  gene          means
  <chr>         <dbl>
1 0610007C21Rik  22.2
> df[c("0610007C21Rik"),]
# A tibble: 1 × 2
  gene  means
  <chr> <dbl>
1 <NA>     NA

CodePudding user response:

Technically your row names are those numbers off to the left, so in this case indexing by row number is the same as indexing by row name. You can change your rownames using the rownames() function, but that's probably not what you want to do. What you probably want to do in this case is find all the rows where the data column "gene" equals the gene you want. For example:

df[df$gene=="0610007C21Rik",]

If you have several different genes you could do this:

df[df$gene == c("0610007C21Rik","0610007L01Rik"),]

If you wanted to use dplyr from the tidyverse you could also do this

library(dplyr)

df %>%
   filter(gene=="0610007C21Rik")

  •  Tags:  
  • r
  • Related