Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcards for filter function in dplyr

Tags:

r

dplyr

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 ...

like image 425
Anna Avatar asked Dec 02 '17 15:12

Anna


2 Answers

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).

like image 92
Ben Bolker Avatar answered Nov 16 '22 07:11

Ben Bolker


What about grepl?

biotype <- biotype %>%
    filter(grepl('^chr', ID))
like image 3
Birger Avatar answered Nov 16 '22 07:11

Birger