Daniele
Daniele

Reputation: 31

Filter by name (in a specific column) failed for specific names

In the dataframe described below, I can filter (by name) for "Signal transduction mechanisms" but I cannot filter for "ABC transporters [PATH:ko02010]" (I get a dataframe with empty rows).

test_2 <- filter(DF, grepl("ABC transporters [PATH:ko02010]", DF$C)) test_1 <- filter(DF, grepl("Signal transduction mechanisms", DF$C))

DF
       B     C
# 1    1    ABC transporters [PATH:ko02010]
# 2    2    ABC transporters [PATH:ko02010]
# 3    2    Signal transduction mechanisms
# 4    5    Signal transduction mechanisms

Upvotes: 1

Views: 24

Answers (1)

TarJae
TarJae

Reputation: 78927

Add fixed = TRUE to grepl:

fixed = TRUE: use exact matching.

test_2 <- filter(df, grepl("ABC transporters [PATH:ko02010]", fixed = TRUE, df$C))

OR We could use %in% Operator:

library(dplyr)

df %>% 
  filter(C %in% "ABC transporters [PATH:ko02010]")
  B                               C
1 1 ABC transporters [PATH:ko02010]
2 2 ABC transporters [PATH:ko02010]

Upvotes: 1

Related Questions