Get all the rows with rownames starting with ABC111
Solution 1:
Given the sample data:
data <- read.table(header=TRUE, row.names=1, sep=" ", text="x col1 col2 col3
ABC111001 12 12 13
ABC111002 3 4 5
ABC000111 7 6 1
ABC000112 9 23 1")
... you can select matching rows using grep
:
> data[grep('^ABC111', rownames(data)),]
col1 col2 col3
ABC111001 12 12 13
ABC111002 3 4 5
Solution 2:
You could use e.g. substr
or grepl
:
df <- read.table(header=TRUE, row.names=1, sep=" ", text="col1 col2 col3
ABC111001 12 12 13
ABC111002 3 4 5
ABC000111 7 6 1
ABC000112 9 23 1")
needle <- "ABC111"
i <- substr(row.names(df), 0, nchar(needle))==needle
i <- grepl(paste0("^", needle), row.names(df))
df[i,]
# col1 col2 col3
# ABC111001 12 12 13
# ABC111002 3 4 5