45

I have the following vector:

X <- c("mama.log", "papa.log", "mimo.png", "mentor.log")

How do I retrieve another vector that only contains elements starting with "m" and ending with ".log"?

Braiam
  • 1
  • 11
  • 47
  • 78
Andrey Adamovich
  • 20,285
  • 14
  • 94
  • 132

4 Answers4

79

you can use grepl with regular expression:

X[grepl("^m.*\\.log", X)]
kohske
  • 65,572
  • 8
  • 165
  • 155
31

Try this:

grep("^m.*[.]log$", X, value = TRUE)
## [1] "mama.log"   "mentor.log"

A variation of this is to use a glob rather than a regular expression:

grep(glob2rx("m*.log"), X, value = TRUE)
## [1] "mama.log"   "mentor.log"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
22

The documentation on the stringr package says:

str_subset() is a wrapper around x[str_detect(x, pattern)], and is equivalent to grep(pattern, x, value = TRUE). str_which() is a wrapper around which(str_detect(x, pattern)), and is equivalent to grep(pattern, x).

So, in your case, the more elegant way to accomplish your task using tidyverse instead of base R is as following.

library(tidyverse)

c("mama.log", "papa.log", "mimo.png", "mentor.log") %>% 
   str_subset(pattern = "^m.*\\.log")

which produces the output:

[1] "mama.log"   "mentor.log"
tjebo
  • 21,977
  • 7
  • 58
  • 94
Alexander Borochkin
  • 4,249
  • 7
  • 38
  • 53
1

Using pipes...

library(tidyverse)

c("mama.log", "papa.log", "mimo.png", "mentor.log") %>%
 .[grepl("^m.*\\.log$", .)]
[1] "mama.log"   "mentor.log"
user3357059
  • 1,122
  • 1
  • 15
  • 30
  • 5
    This adds nothing; it's just an alternate syntax for the function provided in kohske's answer. People who know how to use pipes should know how to adapt the syntax already, and people who don't know hot to use pipes would be confused by this answer as it offers no explanation. – Gregor Thomas Dec 27 '17 at 17:06
  • 1
    But even with a brief explanation of pipes, if someone asked a question "How do I find the average of a vector `x`", and someone answers `mean(x)`, `x %>% mean` is not a different answer. – Gregor Thomas Dec 27 '17 at 17:08
  • Correct. They are not ever *needed*, they are just a matter of style. And using them or not does not change the solution, which is "use `grepl` with these arguments...". – Gregor Thomas Dec 27 '17 at 17:16
  • 2
    I think I need them because I use them often, therefore I added the answer for those who prefer to use pipes. – user3357059 Dec 27 '17 at 17:20