Jeffery Chen
Jeffery Chen

Reputation: 323

Subset data by condition

If I have data look like this:

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

How could I using R let data looks like:(only choose include "GM" data)

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

Upvotes: 3

Views: 65

Answers (1)

akrun
akrun

Reputation: 887901

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

Upvotes: 5

Related Questions