For regular expression to only find at the beginning of the line use ^
at the beginning of the expression. So it is possible:
library(tidyverse)
ES_1_3 <- data_frame(
Pathways = c("REACTOME_final", "inicio_REACTOME_"),
outra_coluna = 1:2
)
ES_1_3[grep("^REACTOME_", ES_1_3$Pathways), ]
#> # A tibble: 1 x 2
#> Pathways outra_coluna
#> <chr> <int>
#> 1 REACTOME_final 1
ES_1_3[str_starts(ES_1_3$Pathways, "REACTOME_"), ]
#> # A tibble: 1 x 2
#> Pathways outra_coluna
#> <chr> <int>
#> 1 REACTOME_final 1
Created on 2019-03-05 by the reprex package (v0.2.1)
Since the latest version of stringr (1.4.0), it is possible to use the function str_starts()
, that appears in the second solution. Using it there is no longer a need to remember the regex symbol that demarcates the beginning of the line.
Note that the result of the two solutions above is the result requested in the question (only cases where they start with the word) and differs from the result of the @Marcusnunes reply, which finds the word in any position of string.
ES_1_3[grep("REACTOME_", ES_1_3$Pathways), ]
#> # A tibble: 2 x 2
#> Pathways outra_coluna
#> <chr> <int>
#> 1 REACTOME_final 1
#> 2 inicio_REACTOME_ 2