Siewmei Loh
Siewmei Loh

Reputation: 45

How to Filter out rows with specific string in R?

V1    V2
P1    1
P1    2
P1    3
P1    4
P1    2
P1    3
P1    4
P2    0
P2    3
P1    1
P3    1
P3    5

Hi all, I wanted to filter out rows from dataset B that contain P1 only (along with the variable V2) to a new table. I've tried

filter(str_detect(B$V1, "P1"))

and `B[grep("P1", rownames(B)), ]

and both doesn't work. It turned out to execute O obs. Thanks! `

Upvotes: 0

Views: 867

Answers (1)

Siewmei Loh
Siewmei Loh

Reputation: 45

Try using

B[B$V1 == "P1", ] 

and it will work perfectly!

Upvotes: 2

Related Questions