I am using dplyr
and I would like to filter my dataframe (biotypes) according to sample IDs which are the first column of the data frame, e.g. they look like this:
ID
chrX.tRNA494-SerAGA
chrX.tRNA636-AlaCGC
mmu_piR_000007
...
I want to filter IDs starting with "chr" from IDs starting with "mmu":
biotype<- biotype %>%
filter( str_detect (biotype, "^chr") ==TRUE )
biotype
Can anyone help please? I am just looking for something like *
that allows me to filter all rows that have a string starting with these particular characters ...
I think you were very close already.
library(stringr)
biotype %>% filter(str_detect(ID,"^chr"))
(you need to specify the column name, and == TRUE
is superfluous).
What about grepl
?
biotype <- biotype %>%
filter(grepl('^chr', ID))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With