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"),]
Author by
Mori
Updated on June 24, 2022Comments
-
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"))