Subset data by condition

If I have data, like this:

A  B    C
1  GM1  100
2  DOX  10
3  GM2  3
4  GM3  99
5  MY   62
6  GMPN 30

      

How can I use R to make the data look like this: (select only "GM" data)

A  B    C
1  GM1  100
3  GM2  3
4  GM3  99

      

+3


source to share


1 answer


you can use grep

 df1[grep('GM\\d+', df1$B),]
 #  A   B   C
 #1 1 GM1 100
 #3 3 GM2   3
 #4 4 GM3  99

      



Or as @ColonelBeauvel mentioned

 subset(df1, grepl('GM\\d+', B))

      

+5


source







All Articles