subset a data frame by row names of different rows

16,216

Using dplyr:

library(dplyr)
DF <- data.frame(row.names=c("12a", "22a", "13a"), Name=c("12","22","13"), plot=c(25,18,9))

If you want to filter by the data frame column "Name", then:

DF.new -> DF %>% filter(Name %in% c("12", "16"))

If you want to filter by actual row.names of the df, then:

DF.new -> DF %>% filter(row.names(DF) %in% c("12a","13a"))

Or, using base R:

DF.new -> DF[DF$Name %in% c("12","13"), ] or

DF.new -> DF[row.names(DF) %in% c("12a","13a"),]

Share:
16,216
Mori
Author by

Mori

Updated on June 24, 2022

Comments

  • Mori
    Mori almost 2 years

    I need to subset lines of data frame according their name. I have tried the following code but it is not working.

    Name  plot  
    12     25
    22     23 
    14     12 
    16     22
    23     54
    
    DF.new <- subset(DF, rownames== c("12" , "16"))