How to subset a vector, based on the condition "contains" a character?
Asked Answered
B

3

6

If I have a vector:

Months = month.abb[1:12]

I want to extract all the months that start with Letter J (in this case, Jan, Jun, and Jul).

  1. Is there a wildcard character, like * in Excel, which lists all elements of vectors which you search for J*?

  2. How do I extract elements that start with either letter 'M' or 'A'. The expected output would be Mar,May,Apr,Aug?

Baa answered 27/6, 2014 at 8:35 Comment(0)
L
7

Try:

 grep("^J", Months,value=TRUE)
 #[1] "Jan" "Jun" "Jul"

grep("^A|^M", Months,value=TRUE)
#[1] "Mar" "Apr" "May" "Aug"
Lamppost answered 27/6, 2014 at 8:36 Comment(1)
Perfect. Worked absolutely flawless. Cheers mate !Baa
R
2

You'll find the glob2rx function helpful for converting wildcard constructions to regular expressions:

> glob2rx("J*")
[1] "^J"
> grep(glob2rx("J*"), Months, value=TRUE)
[1] "Jan" "Jun" "Jul"
Regain answered 27/6, 2014 at 8:39 Comment(2)
Thank you for bringing glob2rx to our attention. Are there other tools in base R that help people create regexes correctly?Bedspread
@user2583119 There aren't any other similar functions that I know of.Regain
C
1

If you happen to have stringr loaded you could do:

library(stringr)

str_subset(Months, "^J")
[1] "Jan" "Jun" "Jul"
Cohesion answered 18/8, 2017 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.